English 中文(简体)
我如何创建用户控制器, 配有三个组合框和不同的数据, 每个组合框都装订在一起?
原标题:How do I create a user control with 3 combo boxes and a different data binding for each?

在最后决定提出这个问题之前,我进行了广泛的搜索。我跟踪了MSDN关于创建用户控制工具的教学,该工具使用简单、复杂和查找数据。

并且它们运作得很好... 对于一个用户控制器,它只使用 单一的组合盒或网景。

现在我要创建一个用户控制器, 有三个不同的组合框。 我要将每个组合框绑在不同的表格上。 这些表格是名称、 类型和产品 。

MSDN 教程涉及为单一的 Combox 创建 DataBindingProperty, 但是没有显示如何对包含多个一个的用户控制进行同样的操作 。

using System.Windows.Forms;
namespace CS
{
    [System.ComponentModel.LookupBindingProperties(
       "DataSource", "DisplayMember", "ValueMember", "LookupMember")]
    public partial class LookupBox : UserControl
    {
        public object DataSource
        {
            get{ return comboBox1.DataSource; }
            set{ comboBox1.DataSource = value; }
        }

        public string DisplayMember
        {
            get{ return comboBox1.DisplayMember; }
            set{ comboBox1.DisplayMember = value; }
        }

        public string ValueMember
        {
            get{ return comboBox1.ValueMember; }
            set{ comboBox1.ValueMember = value; }
        }

        public string LookupMember
        {
            get{ return comboBox1.SelectedValue.ToString(); }
            set{ comboBox1.SelectedValue = value; }
        }

        public LookupBox()
        {
            InitializeComponent();
        }
    }
}

现在,如你所见,代码中只提到一个组合框。 我需要三个组合盒, 每个组合盒都绑在上面提到的不同的表格上。

我的头撞墙。我不太擅长用户控制(尽管我在 ASP.NET 中使用过),但似乎还是个好主意,因为我在应用程序的不同地方 将这三个组合箱放在一起使用。

最佳回答

我会创建一个 user control ,其中包含三个您的 LookupBox s。 例如:

public partial class MyLookupBoxes : UserControl
{
    public LookupBox()
    {
        // Add the 3 LookupBox to this UserControl using the designer
        InitializeComponent();
        SetupDataSources();
    }

    private void SetupDataSources()
    {
        namesLookupBox1.DataSource = names_data_source_1;
        // ...
        typesLookupBox2.DataSource = types_data_srouce_2;
        // ...
        productsLookupBox3.DataSource = products_data_srouce_2;
        // ...
    }
}
问题回答

你可以简单地推断你所知道的 与你需要的东西:

public object DataSource1
{
    get{ return comboBox1.DataSource; }
    set{ comboBox1.DataSource = value; }
}
public object DataSource2
{
    get{ return comboBox2.DataSource; }
    set{ comboBox2.DataSource = value; }
}
public object DataSource3
{
    get{ return comboBox3.DataSource; }
    set{ comboBox3.DataSource = value; }
}

虽然你可能想做比... 1,. 2,. 3. 更好的描述性名称。





相关问题
WPF Datagrid, Setting the background of combox popup

I would like to change the color of the popup background when using a DatagridComboboxColumn in the WPF Toolkit datagrid. I ve edited the Template for a normal Combobox and it works great for selected ...

How to insert ComboBox item into ListBox? [winforms]

The question is very simple, How to insert ComboBox selected item into ListBox using c#? I have tried with this: listbox.Items.Add(combobox.SelectedItem); and some other permutations but it always ...

How do I bind a ComboBox to a one column list

I ve seen how to bind a ComboBox to a list that has columns like this: ItemsSource="{Binding Path=Entries}" DisplayMemberPath="Name" SelectedValuePath="Name" SelectedValue="{Binding Path=Entry}" But ...

Wpf Combobox Limit to List

We are using Wpf Combobox to allow the user to do the following things: 1) select items by typing in the first few characters 2) auto complete the entry by filtering the list 3) suggesting the first ...

热门标签