Tag Archives: WPF

Data Binding 101 with WPF, UWP and Xamarin Forms

Data Binding is a major part of XAML that has been used in Windows WPF, Windows UWP and Xamarin Forms. The biggest benefit with data binding is that it synchronizes your data between your data source and the UI. Changes to the data source will automatically be pushed into the UI and optionally have changes from the UI pushed back to the data source.

The data source must be set in the UI so the binding framework knows the object for the data source. The property that must be set in the UI varies depending on the framework. For WPF and UWP the property is DataContext and for Xamarin Forms the property is BindingContext. The UI object will retrieve the data source from its own property if the property is set. If the UI object DataContext or BindingContext is not set, the binding will look to the UI parent. If the UI parent is not set then it will look at the UI parent’s parent and continue up the tree to the top UI object.

The C# object that is assigned to the UI DataContext or BindingContext needs to inherit from INotifyPropertyChanged. You might need to add a using System.Component Model to be able to use the notify property change.

Any data source properties that will be used for binding need to call the property change notification framework. I use the code block below to implement the INotifyPropertyChanged interface. The first line implements the property changed event. Then we create a method RaisePropertyChanged to check that the property changed event has been hooked.

[pullquote]

public event PropertyChangedEventHandler Property Changed;

private void RaisePropertyChanged([CallerMemberName] string caller = “”)

{

if (this.Property Changed != null)

this.PropertyChanged(this, new PropertyChangedEventArgs(caller)):

}

[/pullquote]

 

The above code method RaisePropertyChanged has one parameter for the name of the property that is changing. We have the parameter defaulted using the code argument [CallerMemberName] in front of the parameter to the name of the property calling the method.

Visual Studio has code snippets that helps you create a property by typing “propfull” and hitting tab twice. Visual Studio will create the code below. The code below includes a field default name of “myVar” and a property with a default name of  “My Property”. Both are defined as an int with the type in the field, the name of the field and the name of the property highlighted allowing you to change them by typing over the defaults and use the tab key to go between each highlighted area.

[pullquote]

private int myVar;
public int MyProperty

{

get { return myVar; }
set { myVar = value; }

}

[/pullquote]

If you were wanting a string field to hold someone’s name, then I would change the myVar to _Name while changing the type int to string and change MyProperty to Name. See sample code below.

[pullquote]

private string _Name;
public int Name

{

get { return _Name; }
set { _Name = value; }

}

[/pullquote]

By default, a property can be used for binding but the value of the property will only be used initially so if the value of the property changes, the UI object will still be using the initial value. To get the UI object to show the updated value then we need the property to call property changed notification. To do this we call the RaisePropertyChanged method in the setter of the property. See the same code below.

[pullquote]

private string _Name;
public int Name

{

get { return _Name; }
set { _Name= value; RaisePropertyChanged(); }

}

[/pullquote]

A lot of apps stop with the above for implementing property change notification but it does something that tends to create extra UI processing at times. The property changed notification is called every time the value is set for the property. That is not a big deal except if the same value is assigned the property change notification is called causing updates to the control which do not need to be made. A simple solution would be to check the new value against the existing value of the property while only updating the field and calling the property change notification if the value is different. See code below.

[pullquote]

private string _Name;
public int Name

{

get { return _Name; }
set
{

If (Name != value)
{

_Name = value; RaisePropertyChanged();

 }

}

}

[/pullquote]

The example above shows a typical simple property for using in data binding.

You can implement binding to your UI object in your C# code or XAML, right now I’ll be showing how to implement the binding in XAML.

Next step would be to discuss the options for implementing the binding in XAML.

To use the property for displaying the Name property in a label, you would put bracket Binding Name and closing bracket inside quotes for the property.

[pullquote]

WPF example
<Label Content=”{Binding Name}”/>
Or
<TextBlock Text=”{Binding Name}”/>

 UWP
<TextBlock Text=”{Binding Name}”/>

 Xamarin Forms example
<Label Text=”{Binding Name}”/>

[/pullquote]

The binding in XAML can have several parts to it but if you just put the name of the property then it defaults the one and only value after the keyword Binding to the “Path”. So, the example above is the same as the following.

[pullquote]

WPF example
<Label Content=”{Binding Path=Name}”/>
Or
<TextBlock Text=”{Binding Path=Name}”/>

UWP
<TextBlock Text=”{Binding Path=Name}”/>

Xamarin Forms example
<Label Text=”{Binding Path=Name}”/>

[/pullquote]

The above example shows one way binding to the label so if the value of the property changes and the property change notification is called then the label would be updated.

Another argument on the binding is mode. Mode shows you to specify how the binding works. OneWay tells the system data flows from the source to the control and is updated by property change notification. OneWayFromSource tells the system to update the data source from the control. OneTime tells the system to update the data source from the control one time and not update even if property change notification is called. TwoWay tells the system the system to update the data source from the control and from the control to the data source.

For mode, you will probably use TwoWay a lot since it is used with most data entry controls in XAML. The TwoWay mode tells the binding to put anything enter by the user into the property in your C# class.

[pullquote]

WPF
<TextBox Text=”{Binding Path=Name, Mode=TwoWay}”/>

UWP
<TextBox Text=”{Binding Path=Name, Mode=TwoWay}”/>

Xamarin Forms
<Entry Text=”{Binding Path=Name, Mode=TwoWay}”/>

[/pullquote]

The above explains basic data binding. Binding has several other parameters that you can use to format the data and bind to other controls or other objects. I will explain more about those later.