有人知道吗?
我上周一直在尝试这个,但没有成功。
现在我可以看到,once 可以成功绑定(bind)到 Button 的 Text 属性,但不能成功绑定(bind)到其 ImageKey 属性:
myButton.Text = "new text"; // really changes the bound data
myButton.ImageKey = "new text"; // does NOT change the bound data
我使用:
myButton.DataBindings.Add ( new Binding ( "ImageKey", this.MyData, "Name", true, DataSourceUpdateMode.OnPropertyChanged ) );
为什么?是什么让绑定(bind)生效?我就是不明白。
编辑:
好的,我为自己的派生控件定义了这些:
public event EventHandler ImageKeyChanged;
protected virtual void OnImageKeyChanged ( EventArgs e )
{
if ( ImageKeyChanged!= null )
{
ImageKeyChanged ( this, e );
}
}
[Bindable ( true )]
public new string ImageKey
{
get
{
return base.ImageKey;
}
set
{
base.ImageKey = value;
this.OnImageKeyChanged ( EventArgs.Empty );
}
}
还是不行。网上有教程或其他东西可以说明这一点吗?它对我来说不起作用。
最佳答案
...does NOT change the bound data
对于 2 向绑定(bind),您需要通知事件 - 这可以采用 2 种常见形式:
- 一个
public event EventHandler {name}Changed;
,其中{name}
是绑定(bind)属性(例如ImageKeyChanged
) - 该对象可以实现
INotifyPropertyChanged
这当然就是对象到控件绑定(bind)的工作原理;我非常确定控制到对象的检测非常相似。请注意,有一个 TextChanged
事件,但没有ImageKeyChanged
事件。
关于c# - 为什么 Winforms.Button.Text 适用于 DataBinding 而不适用于 ImageKey?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1634265/