Dot Net For All

Updating the source using UpdateSourceTrigger(WPF)

In my previous post I have discussed about the data binding and data binding mode in WPF. In this article I will discuss about the UpdateSourceTrigger which provides the synchronization timing among the of source and target data binding with code examples.

Need of Synchronization Timing

Whenever we bind a property to the text property of the textBox control, if the binding mode is set as two way in that case the value of the source property is changed only when the focus is lost from the textbox control. The way binding works for the textbox control is to improve the performance which would otherwise be diminished if we change the source property on every key stroke.

But this is not the case with checkbox and radio button, the source property is updated whenever the state of control is changed.In this post I will discuss about the UpdateSourceTrigger keeping the case of textbox in Mind.

I have created a sample application to demo the UpdateSourceTrigger. The xaml for which is shown below figure.

and the code behind is as shown below

   public partial class MainWindow : Window
    {
        Student student;
        public MainWindow()
        {
            InitializeComponent();
            student = new Student() { Name = "Vikram" } };
            this.DataContext = student;
        }   
    }

    public class Student
    {
        public string Name { get; set; }
    }

Here I have used a textblock which I am binding to the same Name property to shown when the value in the source property is changed.

Types Of UpdateSourceTrigger

  1. Default- For a textbox the default synchronization technique is lost focus. i.e. when the focus is lost from the textBox control source property will be changed.
  2. PropertyChanged- The source property will be changed as keep changing the target property. i.e. on each key press the source property will also change as shown in the code below.
        <StackPanel>
            <TextBox x:Name="tbStudentName" Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}" Width="100" Height="20"/>
            <TextBlock x:Name="tbKStudentName" Text="{Binding Name}" Width="100" Height="20" Margin="2"/>       
            <Button x:Name="btnShow" Click="btnShow_Click" Width="100" Height="30" Content="Show"></Button>
        </StackPanel>

  3. Explicit- The source will never get updates unless we explicitly call the UpdateSource method  on the binding expression. This technique requires the code behind. We will change the UpdateSourceTrigger to explicit as shown in the below figure
    and we have to write some code behind to achieve the whole functionality as shown below.
            private void btnShow_Click(object sender, RoutedEventArgs e)
            {
                BindingExpression be = tbStudentName.GetBindingExpression(TextBox.TextProperty);
                be.UpdateSource();
            }

    As we can see from the above code we are getting the BindingExpression for Text dependencyproperty and calling the UpdateSource method on the same. And hence the source property will be changed after all of this is done.

Here in this article I have discussed about all the ways by which we can synchronize the update of the source property from target property.

 

Top career enhancing courses you can't miss

My Learning Resource

Excel your system design interview