为什么Winforms.Button.Text适用于数据绑定但不是ImageKey?适用于、绑定、不是、数据

2023-09-04 22:54:30 作者:灬阳台漏雨、浇湿我的心

有没有谁知道这个?

我一直在尝试这种在过去的一周,没有运气。

现在我可以看到,一旦可以成功地绑定到一个按钮的Text属性,而不是它的ImageKey属性:

  myButton.Text =新文本; //真正改变绑定的数据
myButton.ImageKey =新文本; //不改变绑定的数据
 

我使用的:

  myButton.DataBindings.Add(新绑定(ImageKey,this.MyData,姓名,真实,DataSourceUpdateMode.OnPropertyChanged));
 
LABVIEW中控件数据绑定中路径应该怎么写

为什么呢?是什么让绑定滴答/工作?我只是不明白这一点。

编辑:

行,所以我定义了这些我自己的派生控件:

 公共事件的EventHandler ImageKeyChanged;

受保护的虚拟无效OnImageKeyChanged(EventArgs的发送)
{
    如果(ImageKeyChanged!= NULL)
    {
    ImageKeyChanged(这一点,E);
    }
}

[可绑定(真)
大众新的字符串ImageKey
{
    得到
    {
    返回base.ImageKey;
    }
    组
    {
    base.ImageKey =价值;
    this.OnImageKeyChanged(EventArgs.Empty);
    }
}
 

它仍然无法正常工作。有上了网,这显示了该教程什么的。它只是不适合我。

解决方案   

...不会更改绑定的数据

2双向绑定,你需要通知的事件 - 这可能需要2常见的形式:

公共事件的事件处理程序(名称)更改; ,其中 {名} 是绑定属性( ImageKeyChanged ,例如) 目标可以实现 INotifyPropertyChanged的

这肯定是如何对象到控制结合的作品;我是pretty的确保控制到对象的检测是非常相似的。请注意,有一个框TextChanged 事件,但没有 ImageKeyChanged 事件。

Is there anyone who knows this?

I have been trying this for the last week, and no luck.

Now I can see that once can bind successfully to a Button's Text property, but not its ImageKey property:

myButton.Text = "new text"; // really changes the bound data
myButton.ImageKey = "new text"; // does NOT change the bound data

I use:

myButton.DataBindings.Add ( new Binding ( "ImageKey", this.MyData, "Name", true, DataSourceUpdateMode.OnPropertyChanged ) );

Why? What makes the Binding tick/work? I just don't get it.

EDIT:

OK so I defined these for my own derived control:

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 );
    }
}

It still doesn't work. Is there a tutorial or something on the net, that shows this. It just doesn't work for me.

解决方案

...does NOT change the bound data

for 2-way binding, you need notification events - this can take 2 common forms:

a public event EventHandler {name}Changed;, where {name} is the bound property (ImageKeyChanged, for example) the object can implement INotifyPropertyChanged

This is certainly how object-to-control binding works; I'm pretty sure that control-to-object detection is very similar. Note that there is a TextChanged event, but no ImageKeyChanged event.