如何执行不同的选择时,多个组合框的绑定到一个共同的来源? [英] How to enforce distinct selections when multiple ComboBox's are bound to a common source?

查看:277
本文介绍了如何执行不同的选择时,多个组合框的绑定到一个共同的来源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的WPF应用程序我有4个组合框。他们都被填充这种方式;

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";



等了 combobox3 combobox4

DT (数据表)已经包含了不同的名称因为我使用获取记录不同的名称。现在我该怎么办,这样当从combobox1选择一个名称不应该是在其他3组合框列表中。

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.

我看了一个问题(的WPF多重组合框的束缚,以一个共同的来源,实施不同的选择),但无法找到一个方法来做到这一点。

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

推荐答案

有关使用一个RowFilter什么?这门课程,如果你使用一个新的数据视图,而不是默认视图只是工作。你可以创建4数据视图,设置的RowFilter当过一个的SelectedItem改变。

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

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

quick and dirty example :)

数据

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

usercontrol.cs

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



XAML

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>

这篇关于如何执行不同的选择时,多个组合框的绑定到一个共同的来源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆