【问题标题】:ComboBox in UserControl. setup the data binding用户控件中的组合框。设置数据绑定
【发布时间】:2011-05-06 00:51:54
【问题描述】:

我在用户控件中有一个组合框,我想对其进行数据绑定,但我在 Visual Studio 2008 设计器视图的属性菜单中唯一可以访问的是数据源和显示成员。有没有办法设置用户控件,以便我也可以在属性菜单中编辑选定的值成员?

[System.ComponentModel.ComplexBindingProperties("DataSource", "DisplayMember")]
public partial class CustomComboBox : UserControl
{
    private object dataSource;
    private string displayMember;


    [AttributeProvider(typeof(IListSource))]
    public object DataSource
    {
        get
        {
            return this.dataSource;
        }

        set
        {
            this.dataSource = value;
        }
    }

    public String DisplayMember
    {
        get
        {
            return this.displayMember;
        }

        set
        {
            this.displayMember = value;
        }
    }

    public CustomComboBox()
    {
        InitializeComponent();
    }

    private void BindComboBox()
    {
        if (this.dataSource == null || this.displayMember == null)
        {
            return;
        }

        Binding binding = new Binding("DataSource", this.dataSource, this.displayMember, true);
        Binding binding2 = new Binding("DisplayMember", this.dataSource, this.displayMember, true);
        this.comboBox1.DataBindings.Clear();
        this.comboBox1.DataBindings.Add(binding);
        this.comboBox1.DataBindings.Add(binding2);
    }
}

【问题讨论】:

    标签: c# data-binding user-controls combobox windows-forms-designer


    【解决方案1】:

    我最终为我想要编辑的每个字段添加了一个属性,并在所有属性之上添加了 [Browsable(true)]。这让我可以在属性菜单中将所有内容编辑为文本字段。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-19
      • 2013-03-18
      • 1970-01-01
      • 1970-01-01
      • 2012-04-18
      • 2012-03-11
      • 1970-01-01
      • 2011-10-22
      相关资源
      最近更新 更多