.net winforms中的DataBound UserControl和PropertyNameChanged事件问题
我有一个用户控件,带有一个文本框和一个数据绑定属性 - 值。 值可以是任何对象,例如颜色、数组、字体等。 只要文本更改,属性值就会更改(只要它有效)。
查看 msdn 文章:如何:应用 PropertyNameChanged 模式 ,它说我应该使用 PropertyNameChanged 事件模式 从控制端。
现在,如果我有 DataSourceUpdateMode = OnValidate,那么我什至不需要应用此模式。如果,说。我的 Value 属性绑定到业务对象中的颜色字段,然后在文本框中键入红色并使用 Tab 键切换到另一个字段后,表单上绑定到同一颜色字段的任何其他字段都会立即更新。
但是,如果我的 DataSourceUpdateMode = OnPropertyChange,我希望表单上的所有其他字段在我按“d”(如 re“d”中所示)后立即更新。
这不会发生。 因此,我应用了前面提到的 PropertyNameChanged 模式,但它仍然没有发生。
但是,如果我删除 PropertyNameChanged 事件并使用 INotifyPropertyChanged,则它可以正常工作。
我的问题是,根据这篇文章, INotifyPropertyChanged 应该由您的业务对象而不是用户控件使用。
它有效,但我不想遇到任何未来的问题。
有人知道发生了什么事吗?
ETA:
假设可以使用 INotifyPropertyChanged,我现在有一个进一步的问题。 我不希望用户必须订阅“PropertyChanged”事件,然后检查参数以查看属性“Value”是否发送了该事件。 所以,我补充说:
Public Event ValueChanged As EventHandler(Of EventArgs)
这打破了绑定。验证时源甚至没有更新,更不用说属性更改了。
我想我必须将 ValueChanged 事件重命名为 FeckingValueChanged 或其他名称。
如果框架实际上首先对引发的 ValueChange 事件做了一些事情,我不会太介意!
I have a UserControl, with a TextBox and a databound property - Value.
Value can be any object such as a Color, Array, Font etc.
Any time the text changes, the property Value is changed as long as it is valid.
Looking at the msdn article: How to: Apply the PropertyNameChanged Pattern
, it says I should use the PropertyNameChanged Event pattern
from the control side.
Now, If I have DataSourceUpdateMode = OnValidate, then I don't even need to apply this pattern. If, say. my Value property is bound to a colour field in a business object, then after I type red, in the textbox and tab to another field, then any other fields on the form, that bind to the same colour field, are updated immediately.
If, however, my DataSourceUpdateMode = OnPropertyChange, I would expect all other fields, on the form, to update as soon as I press 'd' (as in re'd').
This doesn't happen.
So, I apply the aforementioned PropertyNameChanged Pattern and it still doesn't happen.
However, If I remove the PropertyNameChanged event and, instead, use INotifyPropertyChanged, it works perfectly.
My problem is, that according to the article, INotifyPropertyChanged is supposed to be used by your business objects and not the user control.
It works, but I don't want to run into any future problems.
Anyone have any idea what's going on?
ETA:
Assuming it is ok to use INotifyPropertyChanged, I now have a further problem.
I don't want users to have to subscribe to the 'PropertyChanged' event, and then examine the arguments to see if the property 'Value' sent it.
So, I add:
Public Event ValueChanged As EventHandler(Of EventArgs)
This breaks the binding. The source is not even updated on validation, let alone on property change.
I presume I have to rename the ValueChanged event to FeckingValueChanged or something.
I wouldn't mind so much if the framework actually did something with the raised ValueChange event in the first place!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我发现 MSDN 上提供的代码不正确。
下面,我突出显示了不正确的部分并显示了更正:
不正确
正确
我发现您必须以这种精确的方式声明此事件,否则数据绑定 OnPropertyChanged 将无法工作。
例如,由于某种原因,以下内容将不起作用:
Public Event DataSourceChanged As EventHandler(Of EventArgs)
使用 INotifyPropertyChanged 也可以,但您无法同时获得预期的 PropertyNameChanged 事件,而用户将无法获得该事件期望能够订阅。
Ok, I've found that the code supplied at MSDN is incorrect.
Below, I've highlighted the incorrect parts and show the corrections:
Incorrect
Correct
I've found you have to declare this event in this precise way, otherwise databinding, OnPropertyChanged, will not work.
For instance, the following will not work, for some reason:
Public Event DataSourceChanged As EventHandler(Of EventArgs)
Using INotifyPropertyChanged also works, but you can't also have the expected PropertyNameChanged event, to which users would expect to be able to subscribe to.