0

私のWPFアプリには4つのコンボボックスがあります。それらはすべてこのように設定されています。

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

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

と についても同様combobox3ですcombobox4

を使用してレコードを取得しているため、このdt(DataTable) には既に個別の名前が含まれていますdistinct Name。名前がコンボボックス1から選択されたときに、他の3つのコンボボックスリストで使用できないようにするにはどうすればよいですか。

質問(複数のコンボボックスが共通のソースにバインドされ、個別の選択を強制する)を読みましたが、それを行う方法が見つかりませんでした。

4

1 に答える 1

0

RowFilter の使用はどうですか? これはもちろん、DefaultView の代わりに新しい DataView を使用する場合にのみ機能します。4 つの DataView を作成し、SelectedItem が変更されたときに RowFilter を設定できます。

このスレッドで私の答えを見てください。サンプルはリストビュー用ですが、コンボボックスでも同じことが簡単に実現できます

編集:迅速で汚い例:)

データ

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>
于 2012-05-08T08:59:35.913 に答える