[英]OneWay binding on WinForms?
I have a control with a property public MyClass MyProperty{...}
which value is shown on the screen as a graph. 我有一个带有属性public MyClass MyProperty{...}
的控件,该值在屏幕上以图形形式显示。 I want this property to be bindable to any other MyClass
in the program by using the Binding
class ( MyProperty
would be the propertyName
parameter in this Binding
constructor, and the other MyClass
would be the dataMember
parameter) . 我希望通过使用Binding
类将此属性可绑定到程序中的任何其他MyClass
( MyProperty
将是此 Binding
构造函数中的propertyName
参数,而另一个MyClass
将是dataMember
参数)。
MyClass
implements INotifyPropertyChanged
so on that side everything is all right. MyClass
实现了INotifyPropertyChanged
因此在这方面一切都INotifyPropertyChanged
。 But it happens that if I don't implement a get
accessor in MyProperty
and try to bind something to it, I get a "Cannot bind to the property 'MyProperty' on the target control. Parameter name: PropertyName" error. 但是,如果我没有在MyProperty
实现get
访问器并尝试将其绑定到它,则会发生“无法绑定到目标控件上的属性'MyProperty'。参数名称:PropertyName”错误。
Does this mean I have to implement a get
accessor even if I know I will never need to read it's value and I want a OneWay (source to target) binding, and even if I just return null
in the get
accessor? 这是否意味着我必须实现一个get
访问,即使我知道我永远不会需要阅读它的价值,我想一个单向(源到目标)结合,即使我刚刚返回null
的get
访问?
I'm guessing the Binding
class uses this to compare the new value to the old one or to do some other internal stuff. 我猜想Binding
类使用它来将新值与旧值进行比较或进行其他一些内部处理。 I'm not sure, then, if it's a good idea to just return null
, or it would be better to keep always a copy of whatever last object was assigned with the set
accessor and return it in the get
accessor. 那么,我不确定是否只返回null
还是一个好主意,否则最好始终保留使用set
访问器分配的最后一个对象的副本并在get
访问器中返回它。 Maybe I really don't even need to write a get
accessor and I'm doing something else wrong. 也许我真的不需要写get
访问器,而我做错了其他事情。 It just happens that I get the error only when I comment out the get
accessor and stop getting it when I put it back. 碰巧我只有在注释掉get
访问器时才得到错误,而当我放回它时就停止获取它。
Edit: In case there is any confusion: When I say MyProperty
's value is shown on the screen as a graph I don't mean it has a value that some other code reads and show in the screen. 编辑:如果有任何混淆:当我说MyProperty
的值以图形形式显示在屏幕上时,我并不是说它具有其他代码读取并在屏幕中显示的值。 No one reads any value from MyProperty
. 没有人从MyProperty
读取任何值。 MyProperty
's set
accessor is the one that draws stuff on the screen and that's the end of the cycle. MyProperty
的set
访问器是一种在屏幕上绘制内容的访问器,该访问器已结束。
I'm not 100% sure I understand what you mean, but I think the exception you're encountering stems from the Binding
class's CheckBinding
function (reflectored): 我不是100%确信我理解您的意思,但是我认为您遇到的异常源于Binding
类的CheckBinding
函数(反射):
if (descriptor.IsReadOnly && (this.controlUpdateMode != ControlUpdateMode.Never))
{
throw new ArgumentException(SR.GetString("ListBindingBindPropertyReadOnly", new object[] { this.propertyName }), "PropertyName");
}
Therefore, changing the Binding's ControlUpdateMode
to ControlUpdateMode.Never
may be what you're looking for 因此,将绑定的ControlUpdateMode
为ControlUpdateMode.Never
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.