WPF中的互斥组合框

时间:2015-01-04 11:51:47

标签: c# wpf winforms combobox

我有几个Combobox,都共享同一组选项。 如何绑定组合框,以便当我在任何组合框中选择一个选项时,其余选项将不可用?这包括之前选择的组合框 - 如果我要重新选择以前占用的Combobox,除了那些已经在其他组合框中选择的选项外,可用的选项都是。

这里有一个更详细的解释:

  

假设我有3个组合框:Combo1,Combo2,Combo3和4个项目,(" A"," B"," C",&# 34; d&#34)

。      
      
  1. 我选择" A"在Combo1上。然后,Combo2和Combo3将具有" B"," C"和" D"。
  2.   
  3. 然后我选择" C"在Combo2上。然后Combo3会有" B"和" D"可用。
  4.   
  5. 我点击目前在" A"处选择的Combo1。选项" A"," B"和" D"可用。
  6.   

我尝试了以下内容:

private ObservableCollection<string> _ItemList;
public ObservableCollection<string> ItemList
    {
        get
        {
            return _ItemList;
        }
        set
        {
            _ItemList = value;
            NotifyPropertyChanged("ItemList"); 
        }
    }

public void NotifyPropertyChanged(string name)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this,new PropertyChangedEventArgs(name));
        }
    }

然后将每个ComboBox绑定到ItemList。但是,每当我删除在其他组合框中选择的选项时,这些组合框就会变为空白。是每个ComboBox维护不同List的唯一选择,还是有更优雅的解决方案?

2 个答案:

答案 0 :(得分:-1)

private List<string> _ItemList = new List<string>();

private string string1selected; 
private string string2selected;

public IEnemerable<string> ItemList1
{
   get
   {
       return _ItemList.Where(x => x != String2selected);
   }
}
public string String2selected
{
   get
   {
       return string2selected;
   }
   set 
   {
       if (string2selected == value) return;
       NotifypropertyChanged("String2selected");
       NotifypropertyChanged("ItemList1");
   }
}

答案 1 :(得分:-1)

我让Four ComboBoxs像你想要的那样工作,但它是粗糙的版本..

WPF版本。

XAML:

<Window x:Class="TestFourComboboxes.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ComboBox x:Name="Combo_1" IsReadOnly="True" Width="200" Height="32" HorizontalAlignment="Left" Margin="10,50,0,0" VerticalAlignment="Top" SelectionChanged="Combo_1_SelectionChanged"/>
        <ComboBox x:Name="Combo_2" IsReadOnly="True" Width="200" Height="32" HorizontalAlignment="Right" Margin="0,50,10,0" VerticalAlignment="Top" SelectionChanged="Combo_2_SelectionChanged"/>
        <ComboBox x:Name="Combo_3" IsReadOnly="True" Width="200" Height="32" HorizontalAlignment="Left" Margin="10,0,0,50" VerticalAlignment="Bottom" SelectionChanged="Combo_3_SelectionChanged"/>
        <ComboBox x:Name="Combo_4" IsReadOnly="True" Width="200" Height="32" HorizontalAlignment="Right" Margin="0,0,10,50" VerticalAlignment="Bottom" SelectionChanged="Combo_4_SelectionChanged"/>
    </Grid>
</Window>

代码背后:

public partial class MainWindow : Window
{
    string[] list = { "", "A", "B", "C", "D" };
    List<string> list1 = new List<string>() { "", "A", "B", "C", "D" };
    List<string> list2 = new List<string>() { "", "A", "B", "C", "D" };
    List<string> list3 = new List<string>() { "", "A", "B", "C", "D" };
    List<string> list4 = new List<string>() { "", "A", "B", "C", "D" };
    bool ListUpdating = false;


    public MainWindow()
    {
        InitializeComponent();
        foreach (string str in list)
        {
            Combo_1.Items.Add(str);
            Combo_2.Items.Add(str);
            Combo_3.Items.Add(str);
            Combo_4.Items.Add(str);
        }
    }

    private void Combo_1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (ListUpdating == false)
        {
            ListUpdating = true;
            string combo1 = Combo_1.SelectedItem.ToString();
            string combo2 = "";
            string combo3 = "";
            string combo4 = "";

            try
            {
                combo2 = Combo_2.Text;
            }
            catch { }
            try
            {
                combo3 = Combo_3.Text;
            }
            catch { }
            try
            {
                combo4 = Combo_4.Text;
            }
            catch { }


            if (combo1 == "")
            {
                for (int i = 0; i < list1.Count; i++)
                {
                    if (!list2.Contains(list1[i]))
                    {
                        list2.Add(list1[i]);
                        Combo_2.Items.Clear();
                        list2.Sort();

                        for (int u = 0; u < list2.Count; u++)
                        {
                            Combo_2.Items.Add(list2[u]);
                            if (list2[u] == combo2)
                                Combo_2.SelectedIndex = u;
                        }
                    }
                    if (!list3.Contains(list1[i]))
                    {
                        list3.Add(list1[i]);
                        Combo_3.Items.Clear();
                        list3.Sort();

                        for (int u = 0; u < list3.Count; u++)
                        {
                            Combo_3.Items.Add(list3[u]);
                            if (list3[u] == combo3)
                                Combo_3.SelectedIndex = u;
                        }
                    }
                    if (!list4.Contains(list1[i]))
                    {
                        list4.Add(list1[i]);
                        Combo_4.Items.Clear();
                        list4.Sort();

                        for (int u = 0; u < list4.Count; u++)
                        {
                            Combo_4.Items.Add(list4[u]);
                            if (list4[u] == combo4)
                                Combo_4.SelectedIndex = u;
                        }
                    }
                }
            }
            else
            {
                for (int i = 0; i < Combo_2.Items.Count; i++)
                {
                    if (combo1 == Combo_2.Items[i].ToString())
                    {
                        Combo_2.Items.RemoveAt(i);
                        list2.RemoveAt(i);
                        list2.Sort();
                        i = Combo_2.Items.Count;
                    }
                }

                for (int i = 0; i < Combo_3.Items.Count; i++)
                {
                    if (combo1 == Combo_3.Items[i].ToString())
                    {
                        Combo_3.Items.RemoveAt(i);
                        list3.RemoveAt(i);
                        list3.Sort();
                        i = Combo_3.Items.Count;
                    }
                }

                for (int i = 0; i < Combo_4.Items.Count; i++)
                {
                    if (combo1 == Combo_4.Items[i].ToString())
                    {
                        Combo_4.Items.RemoveAt(i);
                        list4.RemoveAt(i);
                        list4.Sort();
                        i = Combo_4.Items.Count;
                    }
                }
            }

            ListUpdating = false;
        }
    }

    private void Combo_2_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (ListUpdating == false)
        {
            ListUpdating = true;
            string combo1 = "";
            string combo2 = Combo_2.SelectedItem.ToString();
            string combo3 = "";
            string combo4 = "";

            try
            {
                combo1 = Combo_1.Text;
            }
            catch { }
            try
            {
                combo3 = Combo_3.Text;
            }
            catch { }
            try
            {
                combo4 = Combo_4.Text;
            }
            catch { }

            if (combo2 == "")
            {
                for (int i = 0; i < list2.Count; i++)
                {
                    if (!list1.Contains(list2[i]))
                    {
                        list1.Add(list2[i]);
                        Combo_1.Items.Clear();
                        list1.Sort();

                        for (int u = 0; u < list1.Count; u++)
                        {
                            Combo_1.Items.Add(list2[u]);
                            if (list1[u] == combo1)
                                Combo_1.SelectedIndex = u;
                        }
                    }
                    if (!list3.Contains(list2[i]))
                    {
                        list3.Add(list2[i]);
                        Combo_3.Items.Clear();
                        list3.Sort();

                        for (int u = 0; u < list3.Count; u++)
                        {
                            Combo_3.Items.Add(list3[u]);
                            if (list3[u] == combo3)
                                Combo_3.SelectedIndex = u;
                        }
                    }
                    if (!list4.Contains(list2[i]))
                    {
                        list4.Add(list2[i]);
                        Combo_4.Items.Clear();
                        list4.Sort();

                        for (int u = 0; u < list4.Count; u++)
                        {
                            Combo_4.Items.Add(list4[u]);
                            if (list4[u] == combo4)
                                Combo_4.SelectedIndex = u;
                        }
                    }
                }
            }
            else
            {
                for (int i = 0; i < Combo_1.Items.Count; i++)
                {
                    if (combo2 == Combo_1.Items[i].ToString())
                    {
                        Combo_1.Items.RemoveAt(i);
                        list1.RemoveAt(i);
                        list1.Sort();
                        i = Combo_1.Items.Count;
                    }
                }

                for (int i = 0; i < Combo_3.Items.Count; i++)
                {
                    if (combo2 == Combo_3.Items[i].ToString())
                    {
                        Combo_3.Items.RemoveAt(i);
                        list3.RemoveAt(i);
                        list3.Sort();
                        i = Combo_3.Items.Count;
                    }
                }

                for (int i = 0; i < Combo_4.Items.Count; i++)
                {
                    if (combo2 == Combo_4.Items[i].ToString())
                    {
                        Combo_4.Items.RemoveAt(i);
                        list4.RemoveAt(i);
                        list4.Sort();
                        i = Combo_4.Items.Count;
                    }
                }
            }
            ListUpdating = false;
        }
    }

    private void Combo_3_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (ListUpdating == false)
        {
            ListUpdating = true;
            string combo1 = "";
            string combo2 = "";
            string combo3 = Combo_3.SelectedItem.ToString();
            string combo4 = "";

            try
            {
                combo1 = Combo_1.Text;
            }
            catch { }
            try
            {
                combo2 = Combo_2.Text;
            }
            catch { }
            try
            {
                combo4 = Combo_4.Text;
            }
            catch { }

            if (combo3 == "")
            {
                for (int i = 0; i < list3.Count; i++)
                {
                    if (!list1.Contains(list3[i]))
                    {
                        list1.Add(list3[i]);
                        Combo_1.Items.Clear();
                        list1.Sort();

                        for (int u = 0; u < list1.Count; u++)
                        {
                            Combo_1.Items.Add(list1[u]);
                            if (list1[u] == combo1)
                                Combo_1.SelectedIndex = u;
                        }
                    }
                    if (!list2.Contains(list3[i]))
                    {
                        list2.Add(list3[i]);
                        Combo_2.Items.Clear();
                        list2.Sort();

                        for (int u = 0; u < list2.Count; u++)
                        {
                            Combo_2.Items.Add(list2[u]);
                            if (list2[u] == combo2)
                                Combo_2.SelectedIndex = u;
                        }
                    }
                    if (!list4.Contains(list3[i]))
                    {
                        list4.Add(list3[i]);
                        Combo_4.Items.Clear();
                        list4.Sort();

                        for (int u = 0; u < list4.Count; u++)
                        {
                            Combo_4.Items.Add(list4[u]);
                            if (list4[u] == combo4)
                                Combo_4.SelectedIndex = u;
                        }
                    }
                }
            }
            else
            {
                for (int i = 0; i < Combo_2.Items.Count; i++)
                {
                    if (combo3 == Combo_2.Items[i].ToString())
                    {
                        Combo_2.Items.RemoveAt(i);
                        list2.RemoveAt(i);
                        i = Combo_2.Items.Count;
                    }
                }

                for (int i = 0; i < Combo_1.Items.Count; i++)
                {
                    if (combo3 == Combo_1.Items[i].ToString())
                    {
                        Combo_1.Items.RemoveAt(i);
                        list1.RemoveAt(i);
                        i = Combo_1.Items.Count;
                    }
                }

                for (int i = 0; i < Combo_4.Items.Count; i++)
                {
                    if (combo3 == Combo_4.Items[i].ToString())
                    {
                        Combo_4.Items.RemoveAt(i);
                        list4.RemoveAt(i);
                        i = Combo_4.Items.Count;
                    }
                }
            }
            ListUpdating = false;
        }
    }

    private void Combo_4_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (ListUpdating == false)
        {
            ListUpdating = true;
            string combo1 = "";
            string combo2 = "";
            string combo3 = "";
            string combo4 = Combo_4.SelectedItem.ToString();

            try
            {
                combo1 = Combo_1.Text;
            }
            catch { }
            try
            {
                combo2 = Combo_2.Text;
            }
            catch { }
            try
            {
                combo3 = Combo_3.Text;
            }
            catch { }

            if (combo4 == "")
            {
                for (int i = 0; i < list4.Count; i++)
                {
                    if (!list1.Contains(list4[i]))
                    {
                        list1.Add(list4[i]);
                        Combo_1.Items.Clear();
                        list1.Sort();

                        for (int u = 0; u < list1.Count; u++)
                        {
                            Combo_1.Items.Add(list1[u]);
                            if (list1[u] == combo1)
                                Combo_1.SelectedIndex = u;
                        }
                    }
                    if (!list2.Contains(list4[i]))
                    {
                        list2.Add(list4[i]);
                        Combo_2.Items.Clear();
                        list2.Sort();

                        for (int u = 0; u < list2.Count; u++)
                        {
                            Combo_2.Items.Add(list2[u]);
                            if (list2[u] == combo2)
                                Combo_2.SelectedIndex = u;
                        }
                    }
                    if (!list3.Contains(list4[i]))
                    {
                        list3.Add(list4[i]);
                        Combo_3.Items.Clear();
                        list3.Sort();

                        for (int u = 0; u < list3.Count; u++)
                        {
                            Combo_3.Items.Add(list3[u]);
                            if (list3[u] == combo3)
                                Combo_3.SelectedIndex = u;
                        }
                    }
                }
            }
            else
            {
                for (int i = 0; i < Combo_2.Items.Count; i++)
                {
                    if (combo4 == Combo_2.Items[i].ToString())
                    {
                        Combo_2.Items.RemoveAt(i);
                        list2.RemoveAt(i);
                        i = Combo_2.Items.Count;
                    }
                }

                for (int i = 0; i < Combo_3.Items.Count; i++)
                {
                    if (combo4 == Combo_3.Items[i].ToString())
                    {
                        Combo_3.Items.RemoveAt(i);
                        list3.RemoveAt(i);
                        i = Combo_3.Items.Count;
                    }
                }

                for (int i = 0; i < Combo_1.Items.Count; i++)
                {
                    if (combo4 == Combo_1.Items[i].ToString())
                    {
                        Combo_1.Items.RemoveAt(i);
                        list1.RemoveAt(i);
                        i = Combo_1.Items.Count;
                    }
                }
            }
            ListUpdating = false;
        }
    }
}