簡體   English   中英

C#如何在沒有Control的情況下實現數據綁定?

[英]C# how to implement databinding without Control?

當兩個類都不是Control類型時,有沒有一種簡單的方法來實現數據綁定?

在我的例子中,我想將變量綁定到自定義ToolStripButton的屬性。

編輯澄清:當綁定到Control時,我可以使用Control的DataBindings集合。 但是,我正在尋找一種綁定屬性的方法,無論源和目標類型如何。

編輯:使用winforms

你可以通過使用Truss來做到這一點。

Truss為實現INotifyPropertyChanged的任何類提供WPF樣式的數據綁定。 它為您提供了更多的靈活性,因為它不會將類限制為從特定的基類派生。

我在ToolStripButton上使用了這個IBindableComponent ,在這里找到。 BindableToolStripButton允許您像使用普通Control一樣使用數據綁定。

[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

}

假設您有一個實現INotifyPropertyChanged的類MyClass ,就像使用綁定到控件屬性時一樣使用它:

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

使用依賴項屬性(您的ToolStripButton中的屬性應該是)並在您的其他類中為您的變量創建一個屬性並創建一個綁定並將其設置為您的ToolstripButton的屬性。

我想這是最簡單的方法。

編輯:那只適用於WPF ...

否則實現INotifyPropertyChanged,當變量發生變化時,它應該在ToolStripButton中自動更改。

對於類似於控件綁定到對象屬性的行為,對於任何類型,您都可以實現相同的接口。

基於這種想法,您可以IBindableComponent ToolStripButton( 或者希望Type具有綁定 )並IBindableComponent實現IBindableComponent 這適用於所有類型的源和目標類型,只要它們沒有sealed 例如,您的工具條按鈕:

public class BindableToolStripButton : ToolStripButton,  IBindableComponent {
    //...

這將導致BindableToolStripButton具有自己的.DataBindings屬性,而基礎ToolStripButton類沒有這樣的屬性。

您需要使用Microsoft在ISiteIBindableComponentIComponent和任何繼承接口中看到的示例來完成實現細節的填寫。

然后,您將Binding實例添加到BindableToolStripButton的任何實例。

(注意:我只有fragements所以將成為我的第一個社區wiki帖子 - 我們會看到這是怎么回事......)

我通過反射寫了一些基本的數據綁定內容。 它適用於任何對象,不需要實現特殊的東西(沒有INotifyPropertyChanged,它只是工作)它是我的編輯器的一部分http://github.com/filipkunc/opengl-editor-cocoa看看HotChocolate / Bindings(喜歡重新實現Cocoa KVC,KVO進入.NET)文件夾。 您可以在HotChocolateTest項目中看到它的運行情況。

還有另一個快速簡單的解決方案,包括在Form中創建屬性,並綁定它們:

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屬性,通知其更改。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM