At the bottom of the GUI, there will be a text field (an entry control) that will allow the user to enter a message. This entry will be data-bound to a property that we will call Text in ChatViewModel. Whenever the user changes the text, this property will be set. This is a classic case of data-binding:
- Add a new private field called text of the string type.
- Add a public property called Text that returns the private text field in the getter and makes a call to the Set() method of the base class in the setter. The Set method is defined in the ViewModel base class and will raise an event back to the view if the property changes in ChatViewModel, effectively keeping them in sync:
private string text;
public string Text
{
get => text;
set => Set(ref text, value);
}
Now, we have a property that we can use for data binding. Let's look at the code we will use to receive messages from ChatService.