Tutorial :WPF: Bindings - Object not updated



Question:

I have a an object which is set to the DataContext in a Window. I have textboxes in the window which are bound to the properties on the object. There seems to be a delay however before the properties on the object are updated.

<TextBox x:Name="txtPropertyOne" Text="{Binding Path=PropertyOne,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"  />  

If I change the values in a few textboxes then quickly try to access the properties to which they map, sometimes there are changes which aren't reflected in the properties of the object. I thought that was what the PropertyChanged UpdateSourceTrigger was supposed to take care of.


Solution:1

If I change the values in a few textboxes then quickly try to access the properties to which they map

I can interpret this statement in two ways:

  1. You're trying to access the values on a background thread. In that case, you may be accessing the properties before the UI thread has had a chance to do its thing.
  2. You're using a separate message on the UI thread to check the values. Bindings are updated at a priority lower than Send and Normal. So if your message is priority Send or Normal it will be processed before any pending binding updates.

If this doesn't answer your question, please clarify what you mean by "quickly trying to access the properties".


Solution:2

The basic rule of WPF Databinding is simple:

  • The target property must be a dependency property, and you're already correct, it's bound to Text property of TextBox.
  • The source property can be a CLR object (other than any derived WPF's DependencyObject), but the object must employ or implement its own INotifyPropertyChanged.

Have you you already implemented INotifyPropertyChanged on your object?


Solution:3

Is your object a DependencyObject? Is the property you're binding to a DependencyProperty? If they are, you don't need to specify the trigger. If they aren't, I'd suggest making them so. Its the easiest way to create a reliably bindable object.

I think the delays are related to your use of the UpdateSourceTrigger. I don't think it does what you think it does, which is why its not behaving as you expect.

Drop the UST, and if that doesn't work refactor your binding object to be a DO and PropertyOne to be a DP. Trust me, once you get through the pains of learning DependencyObjects, its well worth it.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »