表单字段更改WPF时的Fire事件 [英] Fire Event when a form field changes WPF

查看:92
本文介绍了表单字段更改WPF时的Fire事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的我只使用wpf表单。如果用户对表单进行任何更改,我想触发一个事件。我已经将字段绑定到linqtosql datacontext。

让我知道怎么做这件事。

问候

Dear All I am using wpf form. I want to trigger an event if the user make any change on the form. I have already bounded the fields to linqtosql datacontext.
Let me know how do this thing.
Regards

推荐答案

如果看一下控制事件,当发生任何更改时,您将发现没有调用任何事件: https://msdn.microsoft.com/en-us/library/system.windows.controls.control_events%28v=vs.110%29.aspx [ ^ ]。



也许唯一的通用事件是方法 FrameworkElement.OnPropertyChanged ,但问题是:它会捕获太多的事件,包括那些与数据变化无关的事件,例如聚焦状态的变化: https://msdn.microsoft.com/en-us/library/sys tem.windows.frameworkelement.onpropertychanged%28v = vs.110%29.aspx [ ^ ]。



这很自然:这样甚至不会提供非常丰富的信息,因为这种事件的使用并不是特定于已经发生变化的事件,因此过于局限:您只能检测到控件集的状态是脏的。此类事件特定于更具体的控制类型。例如,对于文本框,您可以在列表框中捕获文本更改 - 更改选择,在复选框中 - IsChecked 状态中的更改,依此类推:

https:/ /msdn.microsoft.com/en-us/library/system.windows.forms.control.textchanged(v=vs.110).aspx [ ^ ],

https:// msdn.microsoft.com/en-us/library/system.windows.controls.primitives.selector.selectionchanged(v=vs.110).aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/system .windows.controls.primitives.togglebutton.checked(v = vs.110).aspx [ ^ ],

https://msdn.microsoft.com/en-us/ library / system.windows.controls.primitives.togglebutton.unchecked(v = vs.110).aspx [ ^ ]。



此外,你要做的事情取决于你的目的。如果使用数据绑定,则可能不需要在每个控件级别处理事件,因为您可以在数据更改级别处理事件。请参阅:

https:/ /msdn.microsoft.com/en-us/library/vstudio/ms752347%28v=vs.100%29.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/aa480224.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/aa480226.aspx [ ^ ],

WPF数据绑定 - 第1部分 [ ^ ],

http://www.wpf-tutorial.com/data-binding/introduction [ ^ ],

http://www.wpftutorial .net / DataBindingOverview.html [ ^ ]。< br $>


-SA
If you look at the Control events, you will find no event invoked when "any change" happens: https://msdn.microsoft.com/en-us/library/system.windows.controls.control_events%28v=vs.110%29.aspx[^].

Perhaps the only universal event is the method FrameworkElement.OnPropertyChanged, but the problem is: it will capture too many events, including those not related to change in data, such as changes in focused state: https://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.onpropertychanged%28v=vs.110%29.aspx[^].

This is pretty natural: such even would not be very informative, because the use of such event, not specific to what has changed, would be too limiting: you could only detect that the state of the set of controls is "dirty". Such events are specific to more concrete types of control. For example, for text boxes you can capture the text change, in list box — change of selection, in check box — the change in IsChecked state, and so on:
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.textchanged(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.selector.selectionchanged(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.togglebutton.checked(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.togglebutton.unchecked(v=vs.110).aspx[^].

Also, what you have to do depends on your purpose. If you use data binding, you may not need to take care of the events at the level of each control, because you can handle events at the level of data change. Please see:
https://msdn.microsoft.com/en-us/library/vstudio/ms752347%28v=vs.100%29.aspx[^],
https://msdn.microsoft.com/en-us/library/aa480224.aspx[^],
https://msdn.microsoft.com/en-us/library/aa480226.aspx[^],
WPF Data Binding - Part 1[^],
http://www.wpf-tutorial.com/data-binding/introduction[^],
http://www.wpftutorial.net/DataBindingOverview.html[^].

—SA


这篇关于表单字段更改WPF时的Fire事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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