忽略由于数据绑定而导致的文本/值更改 [英] Ignoring text/value changes due to databinding

查看:131
本文介绍了忽略由于数据绑定而导致的文本/值更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当数据绑定发生时,如何忽略控件的更改?我尝试挂钩各种事件,如getfocus,textchanged和leavefocus,但是如果控件已经具有焦点,并且用户取消其更改,当我重新加载记录和数据绑定接管时,textchanged认为用户仍然进行了更改重点在于控制。调用堆栈为空。是否有任何全局数据绑定事件,如数据绑定开始和数据绑定结束?我看到我在哪里触摸我的OnProperyChanged,但在该调用中,数据绑定不会发生。看起来它正在排队,并在其他一点运行。

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.

有一点,我将在我们的视图模型中挂钩属性更改事件,但是意味着我不会检测到,并且不能VISUALLY显示表单被修改,直到用户离开控制。我知道,我知道,我可以改变我所有的绑定,以便每个字符变化立即发生绑定,但是随着用户还没有完成输入值,这会混淆一些验证案例。

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.

我真的很喜欢像TextChangedByUser这样的事件,会触发用户是否使用键,剪贴板,鼠标剪贴板,用户触发的任何东西。

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.

推荐答案


我真的很喜欢像TextChangedByUser这样的事件,用户是否使用钥匙,剪贴板,鼠标剪贴板,用户触发的任何

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.

不要使用 Text.TextChanged 事件检测用户输入,$
使用 Binding.SourceUpdated 事件。

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

或更一般:
不要使用您的视觉元素的DP来检测用户更新,请改用 Binding.SourceUpdated 事件。

这是一个RoutedEvent。
在绑定时,必须设置 NotifyOnSourceUpdated = true 。在 UpdateSourceTrigger 的帮助下,您甚至可以通知您。

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.



你的xaml可能是这样的:


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
}

(由于评论而编辑)

为什么这是检测用户是否以任何方式触发输入的有效方法??
在给定的示例中,TextBox的DataContext'path'属性是,而TextBox.Text属性是目标

[数据绑定概述] http://msdn.microsoft.com/en-us/library/ms752347.aspx _
TextBox.Text属性在绑定初始化时首次更改,源代码写入TextBox.Text属性。因为您不知道绑定何时发生,您无法使用TextBox.Text属性或其任何事件(例如TextChanged)来检测用户输入。因此:
不要使用 Text.TextChanged 事件来检测用户输入!更一般:不要使用您的视觉元素的DP来检测用户更新!!!

(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!!!

如果用户更改视觉文本字段的内容,这意味着任何,TextBox.Text属性更改(您的目标),之后绑定会在 UpdateSourceTrigger 定义的时间更新 code>。当SourceUpdated事件被触发的时候。

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.

这篇关于忽略由于数据绑定而导致的文本/值更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆