简体   繁体   English

WPF - 为值更改而触发的公共事件

[英]WPF - Common event fired for value changed

Ok. 好。 This seems like an incredibly basic use case but fore some reason I am having an issue finding a common solution across control types. 这似乎是一个令人难以置信的基本用例,但出于某种原因,我遇到了一个问题,即找到跨控件类型的通用解决方案。

Heres the use case: 继承人的用例:

A form is presented to the user with any editable controls. 使用任何可编辑的控件向用户呈现表单。 (Text box, combo, grid etc.). (文本框,组合,网格等)。

The user edits a value in the control and tabs out. 用户在控件中编辑一个值并标签输出。
Expectation is that I can wire to an event like Lost Focus and do "foo" with the changed value. 期望是我可以连接到像迷失焦点这样的事件,并用改变的值做“foo”。

The user then gives focus back to the control and tabs out without making an edit. 然后,用户将焦点返回到控件并标签输出而不进行编辑。 Expectation is that whatever event I am wired to I can check if the value has been changed. 期望无论我连接到哪个事件,我都可以检查该值是否已更改。

Is there one common event across controls that will only fire when the user has finished editing( such as tab out or enter ) and allow me to check previous state vs. current state? 是否有一个跨控件的常见事件只会在用户完成编辑时触发(例如Tab out或enter)并允许我检查以前的状态与当前状态?

Jason, you may want to look into Binding and DependencyProperties in WPF instead of tracking events in your form. Jason,您可能希望在WPF中查看Binding和DependencyProperties,而不是跟踪表单中的事件。 You would bind a class to your form which exposes properties to be changed. 您可以将类绑定到表单,该表单公开要更改的属性。 Using DependancyProperties a single event is fired called "PropertyChanged". 使用DependancyProperties会触发一个名为“PropertyChanged”的事件。

Unfortunately is is a broad topic, but you will really get the full benefit of the WPF programming model. 不幸的是,这是一个广泛的主题,但您将真正获得WPF编程模型的全部好处。 Searches on "dependency properties in wpf" will give you some good examples. 搜索“wpf中的依赖属性”将为您提供一些很好的示例。

I think maybe this is Focus issue. 我想也许这是焦点问题。 There exist two different focus types: keyboard focus and logical focus. 存在两种不同的焦点类型:键盘焦点和逻辑焦点。 The the control that has keyboard focus is the one that has the caret, in what the user press a key and the control process that input. 具有键盘焦点的控件是具有插入符号的控件,用户按下键和输入的控制过程。 The a control may have the logical focus but not having the keyboard focus. 控件可以具有逻辑焦点但不具有键盘焦点。 Please check this in the MSDN article "Input Overview". 请在MSDN文章“输入概述”中查看此内容。 About the other question, maybe you could process the TabControl.SelectedItemChanged for taking the event when a tab item selection changed. 关于另一个问题,也许您可​​以处理TabControl.SelectedItemChanged以在选项卡项选择更改时获取事件。 Hope this is helpful to you... 希望这对你有帮助...

What you may be interested in is implementing INotifyPropertyChanging (not just INotifyPropertyChanged). 您可能感兴趣的是实现INotifyPropertyChanging(不仅仅是INotifyPropertyChanged)。

As noted in the answer of this question , the INotifyPropertyChang ING does not prevent you from changing a value, but to detect WHEN something DOES change, and what it's new value is going to be. 正如在这个问题的答案中所指出的那样 ,INotifyPropertyChang ING不会阻止你改变一个值,而是会检测什么时候会发生变化,以及它的新值是什么。

Hope it helps in your solution needs. 希望它有助于您的解决方案需求。

As the previous answers suggested, you would be better off by implementing a view - viewmodel structure, with your viewmodel having implemented INotifyPropertyChanged, and thus allowing you to bind to properties that will announce their changes to the UI. 正如之前的答案所建议的那样,通过实现view-viewmodel结构,您的视图模型已经实现了INotifyPropertyChanged,从而允许您绑定到将宣布其对UI的更改的属性,您会更好。

If you don't want to do this, you can eventually subscribe on your input elements to the PreviewKeyUp event, check if the Tab key has been pressed and proceed from there. 如果您不想这样做,您最终可以将输入元素订阅到PreviewKeyUp事件,检查是否已按下Tab键并从那里继续。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM