用户控件中的组合框。设置数据绑定

发布于 2024-11-05 02:49:16 字数 1325 浏览 1 评论 0原文

我在用户控件中有一个组合框,我想对其进行数据绑定,但我在 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);
    }
}

I have a combobox in a user control and i want to data bind it but the only things i have access to in the properties menu of the visual studio 2008 designer view is the data source and display member. Is there a way to setup the usercontrol so i can edit the selected value member in the properties menu too?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蝶舞 2024-11-12 02:49:16

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

I ended up adding a property for every field i wanted to edit and added [Browsable(true)] above all the properties. This let me edit everything as a text field in the properties menu.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文