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

时间:2022-05-04 00:06:38

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.

用户在控件中编辑一个值并标签输出。期望是我可以连接到像Lost Focus这样的事件,并用改变的值做“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)并允许我检查以前的状态与当前状态?

4 个解决方案

#1


1  

Jason, you may want to look into Binding and DependencyProperties in WPF instead of tracking events in your form. You would bind a class to your form which exposes properties to be changed. Using DependancyProperties a single event is fired called "PropertyChanged".

Jason,您可能希望在WPF中查看Binding和DependencyProperties,而不是跟踪表单中的事件。您可以将类绑定到表单,该表单公开要更改的属性。使用DependancyProperties会触发一个名为“PropertyChanged”的事件。

Unfortunately is is a broad topic, but you will really get the full benefit of the WPF programming model. Searches on "dependency properties in wpf" will give you some good examples.

不幸的是,这是一个广泛的主题,但您将真正获得WPF编程模型的全部好处。搜索“wpf中的依赖属性”将为您提供一些很好的示例。

#2


0  

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". About the other question, maybe you could process the TabControl.SelectedItemChanged for taking the event when a tab item selection changed. Hope this is helpful to you...

我想也许这就是Focus问题。存在两种不同的焦点类型:键盘焦点和逻辑焦点。具有键盘焦点的控件是具有插入符号的控件,用户按下键和输入的控制过程。 a控件可以具有逻辑焦点但不具有键盘焦点。请在MSDN文章“输入概述”中查看此内容。关于另一个问题,也许您可​​以处理TabControl.SelectedItemChanged以在选项卡项目选择更改时获取事件。希望这对你有帮助......

#3


0  

What you may be interested in is implementing INotifyPropertyChanging (not just INotifyPropertyChanged).

您可能感兴趣的是实现INotifyPropertyChanging(不仅仅是INotifyPropertyChanged)。

As noted in the answer of this question, the INotifyPropertyChangING 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.

正如在这个问题的答案中所指出的那样,INotifyPropertyChangING并不会阻止你改变一个值,而是会检测什么时候会发生变化,以及它的新值是什么。

Hope it helps in your solution needs.

希望它有助于您的解决方案需求。

#4


0  

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键并从那里继续。

#1


1  

Jason, you may want to look into Binding and DependencyProperties in WPF instead of tracking events in your form. You would bind a class to your form which exposes properties to be changed. Using DependancyProperties a single event is fired called "PropertyChanged".

Jason,您可能希望在WPF中查看Binding和DependencyProperties,而不是跟踪表单中的事件。您可以将类绑定到表单,该表单公开要更改的属性。使用DependancyProperties会触发一个名为“PropertyChanged”的事件。

Unfortunately is is a broad topic, but you will really get the full benefit of the WPF programming model. Searches on "dependency properties in wpf" will give you some good examples.

不幸的是,这是一个广泛的主题,但您将真正获得WPF编程模型的全部好处。搜索“wpf中的依赖属性”将为您提供一些很好的示例。

#2


0  

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". About the other question, maybe you could process the TabControl.SelectedItemChanged for taking the event when a tab item selection changed. Hope this is helpful to you...

我想也许这就是Focus问题。存在两种不同的焦点类型:键盘焦点和逻辑焦点。具有键盘焦点的控件是具有插入符号的控件,用户按下键和输入的控制过程。 a控件可以具有逻辑焦点但不具有键盘焦点。请在MSDN文章“输入概述”中查看此内容。关于另一个问题,也许您可​​以处理TabControl.SelectedItemChanged以在选项卡项目选择更改时获取事件。希望这对你有帮助......

#3


0  

What you may be interested in is implementing INotifyPropertyChanging (not just INotifyPropertyChanged).

您可能感兴趣的是实现INotifyPropertyChanging(不仅仅是INotifyPropertyChanged)。

As noted in the answer of this question, the INotifyPropertyChangING 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.

正如在这个问题的答案中所指出的那样,INotifyPropertyChangING并不会阻止你改变一个值,而是会检测什么时候会发生变化,以及它的新值是什么。

Hope it helps in your solution needs.

希望它有助于您的解决方案需求。

#4


0  

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键并从那里继续。