Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#如何在没有控制的情况下实现数据绑定?_C#_Winforms_Data Binding - Fatal编程技术网

C#如何在没有控制的情况下实现数据绑定?

C#如何在没有控制的情况下实现数据绑定?,c#,winforms,data-binding,C#,Winforms,Data Binding,当两个类都不是Control类型时,有没有一种简单的方法来实现数据绑定 在本例中,我希望将变量绑定到自定义ToolStripButton的属性 编辑澄清:当绑定到控件时,我可以使用控件的数据绑定集合。但是,我正在寻找一种绑定属性的方法,而不考虑源和目标类型 编辑:使用winforms使用依赖属性(ToolStripButton中的属性应为),在其他类中为变量创建属性,创建绑定并将其设置为ToolStripButton的属性 我想这大概是最简单的方法了 编辑:这只适用于WPF 否则实现INotif

当两个类都不是Control类型时,有没有一种简单的方法来实现数据绑定

在本例中,我希望将变量绑定到自定义ToolStripButton的属性

编辑澄清:当绑定到控件时,我可以使用控件的数据绑定集合。但是,我正在寻找一种绑定属性的方法,而不考虑源和目标类型

编辑:使用winforms使用依赖属性(ToolStripButton中的属性应为),在其他类中为变量创建属性,创建绑定并将其设置为ToolStripButton的属性

我想这大概是最简单的方法了

编辑:这只适用于WPF


否则实现INotifyPropertyChanged,当变量更改时,它应该在ToolStripButton中自动更改

您可以使用


Truss为实现INotifyPropertyChanged的任何类提供WPF样式的数据绑定。它在这方面给了您更多的灵活性,因为它不限制类从特定的基类派生。

对于类似的行为,如控件绑定到对象属性,对于任何类型,您都可以实现相同的接口

基于这种思想,您可以将ToolStripButton子类化(或具有绑定的所需类型),并为其实现
IBindableComponent
。这适用于所有类型的源和目标类型,只要它们不是密封的。例如,工具条按钮:

public class BindableToolStripButton : ToolStripButton,  IBindableComponent {
    //...
这将导致BindableToolStripButton拥有自己的
.DataBindings
属性,而基本ToolStripButton类没有这样的属性

您需要使用Microsoft for中的示例以及任何继承的接口来填写实现细节

然后将添加到BindableToolStripButton的任何实例中


(注意:我只有碎片,所以我将发表我的第一篇社区wiki帖子-我们将看看这是怎么回事…

我通过反思写了一些基本的数据绑定内容。它可以在任何对象上工作,并且不需要实现特殊的功能(没有INotifyPropertyChanged,它只是工作而已)。它是我在look at HotChocolate/Bindings(如将Cocoa KVC、KVO重新实现到.NET)文件夹中的编辑器的一部分。您可以在Hotchocolatest项目中看到它的实际应用。

我在找到的ToolStripButton上使用此实现。
BindableToolStripButton
允许您像使用普通
控件一样使用数据绑定

[ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.ToolStrip | ToolStripItemDesignerAvailability.StatusStrip)]
public class BindableToolStripButton : ToolStripButton, IBindableComponent
{

    public BindableToolStripButton()
        : base() { }
    public BindableToolStripButton(String text)
        : base(text) { }
    public BindableToolStripButton(System.Drawing.Image image)
        : base(image) { }
    public BindableToolStripButton(String text, System.Drawing.Image image)
        : base(text, image) { }
    public BindableToolStripButton(String text, System.Drawing.Image image, EventHandler onClick)
        : base(text, image, onClick) { }
    public BindableToolStripButton(String text, System.Drawing.Image image, EventHandler onClick, String name)
        : base(text, image, onClick, name) { }



    #region IBindableComponent Members
    private BindingContext bindingContext;
    private ControlBindingsCollection dataBindings;

    [Browsable(false)]
    public BindingContext BindingContext
    {
        get
        {
            if (bindingContext == null)
            {
                bindingContext = new BindingContext();
            }
            return bindingContext;
        }
        set
        {
            bindingContext = value;
        }
    }
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public ControlBindingsCollection DataBindings
    {
        get
        {
            if (dataBindings == null)
            {
                dataBindings = new ControlBindingsCollection(this);
            }
            return dataBindings;
        }
    }
    #endregion

}
假设您有一个类
MyClass
实现,请像绑定到控件属性时一样使用它:

bindableToolStripButton1.DataBindings.Add("Enabled", myClass1, "MyBooleanProperty");

还有另一个快速简单的解决方案,它包括在表单中创建属性并绑定它们:

public MyForm : Form
{
    ...

    public bool CanDelete
    { 
        get { return deleteToolStripButton.Enabled; }
        set { deleteToolStripButton.Enabled = value; }
    }

    public MyForm()
    {
        ...
        this.DataBindings.Add("CanDelete", this.MyModel, "DeleteAllowed",
                              false, DataSourceUpdateMode.Never);
        ...
    }
}

假设MyModel包含通知其更改的DeleteAllowed属性。

ToolStripButton暗示WinForms。@bytenik暗示不够好,请参见已有一个仅与WPF相关的答案。Hmmm INotifyPropertyChanged似乎可以做到这一点,但这也意味着我必须自己处理这两者之间的同步,对吗?即在两个方向上处理PropertyChanged事件。如果是这样的话,这就不会像数据绑定一样了,我想…但是嘿。。。谢谢你给我指出这个界面。这对于另一个任务来说是非常好的:)太好了。谢谢你的链接。我实际上并没有使用Truss,但是它为我指明了正确的方向,我可以使用我自己的copmact数据绑定类:)你能给我们展示一些代码吗?我需要在两个不同的自定义类中绑定一个属性。谢谢,您的评论非常有用。然而,我决定不使用这种技术,因为在我的例子中,有许多不同的类必须实现这些接口。因此,基于反射和INotifyPropertyChanged(这比IBindableComponent更容易实现),我推出了自己的。不幸的是,当我完成自己的简单反思解决方案时,我刚刚看到了你的帖子。但是,如果我需要其他功能,我一定会检查这个,谢谢!