UserControl中的ComboBox。设置数据绑定

时间:2011-05-06 00:51:55

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

我在用户控件中有一个组合框,我想要数据绑定它,但我在visual studio 2008设计器视图的属性菜单中访问的唯一内容是数据源和显示成员。有没有办法设置usercontrol所以我也可以在属性菜单中编辑选定的值成员?

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

1 个答案:

答案 0 :(得分:0)

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