A poster on MSDN Forums asked a question about data binding. I had the same issue later when I was working on my query base editing tool. The question goes something like this
There are two textboxes A, B and C. TextBox A and B are bound to some data source. TextBox C has value which is bound to a converter which sends the value based on the content inside A and B. The first time the form shows up, C looks fine. Now, I go ahead and edit the text in one of the A or B. The C does not get updated until I reload the section(form). C is basically data bound to A and B. So how do I update the databound value on C?
The question or scenario is not exactly as described above, but I hope it is simple enough to give a clear picture.
Let me try and explain what happens.
When the form is loaded, the data bound controls are updated. Then the changes made are not reflected onto the C, but the actual source is updated. So we need to add some framework that updates even C.
The Binding element has a property called "NotifyOnSourceUpdated". If you set this value on Binding to True and then handle the SourceUpdated event on that data bound element, you can track the changes made from the control. Doing so, we can then refresh the data binding on C. The Xaml for A, B and C can look something like this:
<StackPanel DataContext={Binding MyStupidDataSource}>
<
SourceUpdated="RefreshContentsOfC"/>
<
SourceUpdated="RefreshContentsOfC"/>
<
</
Now the RefreshContentsOfC is an event handler which refreshes the data binding on C. The code would look like.
public void RefreshContentsOfC(object sender, DataTransferEventArgs e){
//Refresh Data Binding -> Answer for the question
BindingOperations.GetBindingExpressionBase(C, TextBox.TextProperty).UpdateTarget();
}
No comments:
Post a Comment