多个Combobox具有相同的Itemsource但SelectedItem应该是不同的 - WPF

时间:2016-05-18 10:34:01

标签: wpf xaml data-binding combobox observablecollection

目标是多个Combobox,一旦在任何Combobox中选择了该项,就应将其从其他Combobox中移除或隐藏供选择。

我可以在任何Combobox es上更改选择时更改源。但是问题是当我从ItemsSource中删除所选项目时,项目已从所有来源中删除,包括我选择的项目。

以下是样本

XAML

<StackPanel>
    <ComboBox Width="100" Height="50" SelectedItem="{Binding SelectedItem}" ItemsSource="{Binding ComboboxItems}" />
    <ComboBox Width="100" Height="50" SelectedItem="{Binding SelectedItem}" ItemsSource="{Binding ComboboxItems}" />
    <ComboBox Width="100" Height="50" SelectedItem="{Binding SelectedItem}" ItemsSource="{Binding ComboboxItems}" />
    <ComboBox Width="100" Height="50" SelectedItem="{Binding SelectedItem}" ItemsSource="{Binding ComboboxItems}" />
</StackPanel>

代码隐藏

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        ObservableCollection<string> s = new ObservableCollection<string>();
        s.Add("item1");
        s.Add("item2");
        s.Add("item3");
        s.Add("item4");
        s.Add("item5");
        s.Add("item6");

        DataContext = new MainViewModel() { ComboboxItems = s , SelectedItem = "item2" };

    }
}

public class MainViewModel : INotifyPropertyChanged
{
    private ObservableCollection<string> comboboxitems;

    public ObservableCollection<string> ComboboxItems
    {
        get { return comboboxitems; }  
        set { comboboxitems = value; OnPropertyChanged("ComboboxItem"); }
    }

    private string _selectedItem;

    public string SelectedItem
    {
        get { return _selectedItem; }
        set
        {
            _selectedItem = value;
            comboboxitems.Remove(value);  //here removing selected item from itemssource
            OnPropertyChanged("SelectedItem");
        }
    }


    public event PropertyChangedEventHandler PropertyChanged;

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

我知道每个Combobox我可以拥有多个收藏集,但如果Combobox的数量增加,可能需要大量内存。

希望,在WPF中应该有一种简单的方法来实现这一点。

1 个答案:

答案 0 :(得分:4)

您可以为每个SelectedItem定义不同的ComboBox,然后为每个SharedItemSource创建ComboBox的包装,以过滤掉其他SelectedItem ComboBox。例如:

C#:

    public IEnumerable<string> ComboboxItems1
    {
        get
        {
            return ComboboxItems.Where(x => x != SelectedItem2 && x != SelectedItem3);
        }
    }

    public string SelectedItem1
    {
        get { return _selectedItem1; }
        set
        {
            if (_selectedItem1 != value)
            {
                _selectedItem1 = value;
                RaisePropertyChanged("SelectedItem1");
                RaisePropertyChanged("ComboboxItems2"); //raise propertychanged to refresh GUI 
                RaisePropertyChanged("ComboboxItems3");
            }
        }
    }       

    public IEnumerable<string> ComboboxItems2
    {
        get
        {
            return ComboboxItems.Where(x => x!=SelectedItem1&&x!=SelectedItem3);
        }
    }

    public string SelectedItem2
    {
        get { return _selectedItem2; }
        set
        {
            if (_selectedItem2 != value)
            {
                _selectedItem2 = value;                   
                RaisePropertyChanged("SelectedItem2");
                RaisePropertyChanged("ComboboxItems1"); //raise propertychanged to refresh GUI
                RaisePropertyChanged("ComboboxItems3");
            }
        }
    }

    public IEnumerable<string> ComboboxItems3
    {
        get
        {
            return ComboboxItems.Where(x => x != SelectedItem1 && x != SelectedItem2);
        }
    }

    public string SelectedItem3
    {
        get { return _selectedItem3; }
        set
        {
            if (_selectedItem3 != value)
            {
                _selectedItem3 = value;                 
                RaisePropertyChanged("SelectedItem3");
                RaisePropertyChanged("ComboboxItems1"); //raise propertychanged to refresh GUI
                RaisePropertyChanged("ComboboxItems2");
            }
        }
    }

XAML:

<ComboBox SelectedItem="{Binding SelectedItem1}" ItemsSource="{Binding ComboboxItems1}" />
<ComboBox  SelectedItem="{Binding SelectedItem2}" ItemsSource="{Binding ComboboxItems2}" />
<ComboBox  SelectedItem="{Binding SelectedItem3}" ItemsSource="{Binding ComboboxItems3}" />