Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignoring text/value changes due to databinding

How does one ignore changes to a control when databinding occurs? I tried hooking various events like gotfocus,textchanged,and leavefocus, but if the control already has focus and the user "cancels" their changes, when I reload the record and data binding takes over, textchanged thinks the user still made the change since the focus is on that control. The call stack is empty. Are there any global data binding events like databinding starting and databinding ending? I see where I fire my OnProperyChanged but within that call, databinding does not occur. Looks like it's getting "queued" up and runs at some other point.

At one point, I was going to hook the property change events in our view model , but this means I won't detect and can't VISUALLY display the form is modified till the user leaves the control. I know, I know, I can change all my bindings so that binding occurs immediately on every character change but then this messes with some validation cases as the user hasn't finished typing in their value.

I'd really love some kind of event like TextChangedByUser that would fire whether the user used a key, clipboard, mouse clipboard, anything triggered by the user.

I just can't figure out how to distinguish between user changes and databinding changes.

like image 212
happyfirst Avatar asked Oct 20 '11 19:10

happyfirst


1 Answers

I'd really love some kind of event like TextChangedByUser that would fire whether the user used a key, clipboard, mouse clipboard, anything triggered by the user.

I just can't figure out how to distinguish between user changes and databinding changes.

Don't use the Text.TextChanged event to detect user input,
use the Binding.SourceUpdated event instead.

Or more general: Don't use the DPs of your visual elements to detect user updates, use the Binding.SourceUpdated event instead.

This is a RoutedEvent. At your binding, you have to set NotifyOnSourceUpdated = true. With help of UpdateSourceTrigger you are even able to finetune when you want to be informed.


Your xaml could be sth like this:
<Grid x:Name="LayoutRoot" Binding.SourceUpdated="LayoutRoot_SourceUpdated">
    ...
    <TextBox>
        <TextBox.Text>
            <Binding NotifyOnSourceUpdated="True" Path="path" UpdateSourceTrigger="PropertyChanged" >
            </Binding>
        </TextBox.Text>
</Grid>

Your event could be like this:

private void LayoutRoot_SourceUpdated(object sender, DataTransferEventArgs e)
{
    // called every time s.th. changed by user
}

(edited due to comment)
Why is this a valid way to detect if an input is triggered in any way by the user?
In the given example, the TextBox's DataContext 'path' property is the source, while the 'TextBox.Text' property is the target.
[Data Binding Overview]http://msdn.microsoft.com/en-us/library/ms752347.aspx
The TextBox.Text property is changed for the first time when the binding initializes and the source-value is written to the 'TextBox.Text' property. Because you do not know when the binding takes place exactly you cannot use the TextBox.Text property or any of its events (e.g. TextChanged) to detect a user input. Hence: Don't use the Text.TextChanged event to detect user input!!! more general: Don't use the DPs of your visual elements to detect user updates!!!

If the user changes the content of the visual text field by which means whatsoever, the 'TextBox.Text' property changes (your target).After that, the binding updates the source at a time defined by UpdateSourceTrigger.That's when the SourceUpdated event is fired.

I admit not to know the effect of changes to the binding source from outside the binding. But I have a complete Editor-like Desktop-Application detecting changes by the user that way and it is working very nicely.

like image 151
Martin Meeser Avatar answered Oct 14 '22 03:10

Martin Meeser