当多个ComboBox绑定到公共源时,如何强制执行不同的选择?

时间:2012-05-08 08:43:11

标签: c# wpf combobox wpf-controls

在我的WPF应用程序中,我有4个组合框。他们都是这样填充的;

combobox1.ItemSource = dt.DefaultView;
combobox1.DisplayMemberpath = "Name";

combobox2.ItemSource = dt.DefaultView;
combobox2.DisplayMemberpath = "Name";

combobox3combobox4

当我使用dt获取记录时,此distinct Name(DataTable)已包含不同的名称。现在该怎么办,当从combobox1中选择一个名字时,它不应该在其他3个组合框列表中可用。

我读了一个问题(Multiple ComboBoxes bound to a common source, enforcing distinct selections),但找不到办法。

1 个答案:

答案 0 :(得分:0)

使用RowFilter怎么样?这当然只是使用新的DataView而不是DefaultView。你可以创建4个DataView,并在SelectedItem发生变化时设置RowFilter。

this帖子中查看我的答案。该示例适用于listview,但您可以轻松地使用combobox实现相同的

编辑:快速而肮脏的例子:)

数据

public class MyTest
{
    private DataTable dt;

    public BindingListCollectionView View1 { get; set; }
    public BindingListCollectionView View2 { get; set; }
    public BindingListCollectionView View3 { get; set; }
    public BindingListCollectionView View4 { get; set; }

    private string _selected1;
    public string Selected1
    {
        get { return _selected1; }
        set { _selected1 = value;
            this.UpdateFilter();
        }
    }

    private void UpdateFilter()
    {
        this.View1.CustomFilter = GetFilter(this.Selected2, this.Selected3, this.Selected4);
        this.View2.CustomFilter = GetFilter(this.Selected1, this.Selected3, this.Selected4);
        this.View3.CustomFilter = GetFilter(this.Selected1, this.Selected2, this.Selected4);
        this.View4.CustomFilter = GetFilter(this.Selected1, this.Selected2, this.Selected3);
    }

    private string GetFilter(string selected2, string selected3, string selected4)
    {
        var filter = "";

        if (!string.IsNullOrWhiteSpace(selected2))
            filter = "Name <> '" + selected2 + "' and ";

        if(!string.IsNullOrWhiteSpace(selected3))
            filter += "Name <> '" + selected3 + "' and ";

        if (!string.IsNullOrWhiteSpace(selected4))
            filter += "Name <> '" + selected4 + "' and ";

        if (!string.IsNullOrWhiteSpace(filter))
            filter = filter.Substring(0, filter.Length - 4);

        return filter;
    }

    private string _selected2;
    public string Selected2
    {
        get { return _selected2; }
        set { _selected2 = value;
        this.UpdateFilter();
        }
    }

    private string _selected3;
    public string Selected3
    {
        get { return _selected3; }
        set { _selected3 = value;
        this.UpdateFilter();
        }

    }
    private string _selected4;
    public string Selected4
    {
        get { return _selected4; }
        set { _selected4 = value;
        this.UpdateFilter();
        }

    }

    public MyTest()
    {
        this.dt = new DataTable();
        this.dt.Columns.Add("Name");

        for (int i = 0; i < 15; i++)
        {
            var row = dt.NewRow();
            row["Name"] = "Name " + i;
            dt.Rows.Add(row);
        }

        View1 = new BindingListCollectionView(new DataView(dt));
        View2 = new BindingListCollectionView(new DataView(dt));
        View3 = new BindingListCollectionView(new DataView(dt));
        View4 = new BindingListCollectionView(new DataView(dt));
    }
}

usercontrol.cs

public partial class ComboxFour : UserControl
{
    private MyTest data;
    public ComboxFour()
    {
        this.data = new MyTest();
        InitializeComponent();
        this.DataContext = data;
    }
}

XAML

<StackPanel Orientation="Horizontal">
    <ComboBox Width="100" Height="30" SelectedValue="{Binding Selected1}" ItemsSource="{Binding View1}" DisplayMemberPath="Name" SelectedValuePath="Name"/>
    <ComboBox Width="100" Height="30" SelectedValue="{Binding Selected2}" ItemsSource="{Binding View2}" DisplayMemberPath="Name" SelectedValuePath="Name"/>
    <ComboBox Width="100" Height="30" SelectedValue="{Binding Selected3}" ItemsSource="{Binding View3}" DisplayMemberPath="Name" SelectedValuePath="Name"/>
    <ComboBox Width="100" Height="30" SelectedValue="{Binding Selected4}" ItemsSource="{Binding View4}" DisplayMemberPath="Name" SelectedValuePath="Name"/>
</StackPanel>