Ignoring text/value changes due to databinding

后端 未结 3 1547
青春惊慌失措
青春惊慌失措 2021-01-18 07:38

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 fo

相关标签:
3条回答
  • 2021-01-18 08:15

    You can use the UIElement.TextInput event to detect user input.

    Note that the event is probably already handled by the input control itself so you might have to use the UIElement.PreviewTextInput event.

    0 讨论(0)
  • 2021-01-18 08:24

    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.

    0 讨论(0)
  • 2021-01-18 08:24

    You should update your Binding Code to set the following

    {Binding Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}
    

    EDIT: Sorry, I have overseen the fact that you already know this...In that case, I can't help :(

    0 讨论(0)
提交回复
热议问题