How to enforce distinct selections when multiple C

2019-06-10 20:39发布

In my WPF app i have 4 comboboxes. They all are populated this way;

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

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

and so on for combobox3 and combobox4.

This dt (DataTable) already contains distinct Names as I am fetching records using distinct Name. Now what should I do so that when a Name is selected from combobox1 it should not be available in other 3 comboboxes list.

I read a question(Multiple ComboBoxes bound to a common source, enforcing distinct selections) but couldn't find a way to do it.

1条回答
爷、活的狠高调
2楼-- · 2019-06-10 20:49

what about using a RowFilter? this of course just work if you use a new DataView instead of the DefaultView. you could create 4 DataView and set the RowFilter when ever a SelectedItem changed.

look at my answer in this thread. the sample is for listview, but you can easy achieve the same with combobox

EDIT: quick and dirty example :)

data

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>
查看更多
登录 后发表回答