如何在自定义集合中使用所选项目

时间:2013-10-10 18:21:24

标签: c# wpf combobox observablecollection selecteditem

我有一个程序可以更改comboBox中显示的项目集合。我之前在this question谈过这个问题。无论如何,我现在正在使用自定义集合,因为observableCollection没有selectedItem属性。自定义集合中包含selectedItem属性,但我不确定如何设置它以便保存数据。

自定义集合类

public class MyCustomCollection<T>: ObservableCollection<T>
{
    private T _mySelectedItem;

    public MyCustomCollection(IEnumerable<T> collection) : base(collection) { }

    public T MySelectedItem
    {
        get { return _mySelectedItem; }
        set
        {
            if (Equals(value, _mySelectedItem)) return;
            _mySelectedItem = value;
            OnPropertyChanged(new PropertyChangedEventArgs("MySelectedItem"));
        }
    }
}

ViewModel - 我更改comboBox中的集合并为每个集合设置selectedItem

//Property for the selected command in the LIST BOX
//**For clarity: the collection in the comboBox is changed based on what is
//selected in the list box
public string SelectedCommand
{
    get { return _selectedCommand; }
    set
    {
        _selectedCommand = value;
        NotifyPropertyChange(() => SelectedCommand);

        if (SelectedCommand == "1st Collection of type A")
        {
            ComboBoxEnabled = true;
            ComboBoxList = new MyCustomCollection<string>(collectionA);
            //collectionA.MySelectedItem = ??(What would I put here?) 
        }
        if (SelectedCommand == "2nd Collection of type A")
        {
            ComboBoxEnabled = true;
            ComboBoxList = new MyCustomCollection<string>(collectionA);
            //collectionA.MySelectedItem = ??(What would I put here?)
        }
    }
}

如何为我创建并添加到MySelectedItem的每个新集合为comboBox分配值?这样就可以在每次切换到comboBox中的其他集合时显示selectedItem

更新

我的收藏集现已设为ObservableCollection<string>

ListBoxComboBox

的XAML
<ListBox ItemsSource="{Binding Model.CommandList}" SelectedItem="{Binding Model.SelectedCommand}" ... />

<ComboBox ItemsSource="{Binding Model.ComboBoxList}" SelectedItem="{Binding Model.SelectedOperation}"  ... />

** SelectedCommand下的新ListBox媒体资源:

public string SelectedCommand
{
    get { return _selectedCommand; }
    set
    {
        _selectedCommand = value;
        NotifyPropertyChange(() => SelectedCommand);

        switch (SelectedCommand)
        {
            case "Collection A":
                {
                    ComboBoxList = CollectionA;
                    break;
                }
            case "Collection B":
                {
                    ComboBoxList = CollectionB;
                    break;
                }
        }
        NotifyPropertyChange(() => ComboBoxList);
    }
}

该程序仍然没有保留为每个集合选择的selectedItem我必须忘记或不理解某些内容。

1 个答案:

答案 0 :(得分:0)

您不需要自定义集合来绑定所选项目。 以下是如何完成此操作的示例:

   <StackPanel>
        <!-- your listbox -->
        <ListBox x:Name="listbox">
            <sys:String>Fruits</sys:String>
            <sys:String>Vegetables</sys:String>
            <sys:String>Others</sys:String>
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectionChanged">
                    <i:InvokeCommandAction Command="{Binding SelectionChangedCommand}" CommandParameter="{Binding ElementName=listbox, Path=SelectedItem}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </ListBox>

        <!-- your combo box -->
        <ComboBox ItemsSource="{Binding Items}" SelectedItem="{Binding CurrentItem}"/>
    </StackPanel>

xaml包含一个带有项目的ListBox(在本例中为简单字符串)和一个命令SelectionChangedCommand,只要列表中的选择发生变化就会调用它。

这是 ViewModel

public class MainWindowViewModel : NotificationObject
{
    // your collections
    private ObservableCollection<string> fruits;
    private ObservableCollection<string> vegetables;
    private ObservableCollection<string> others;

    public MainWindowViewModel()
    {
        fruits = new ObservableCollection<string>
            {
                "Apple", "Banana"
            };
        vegetables = new ObservableCollection<string>
            {
                "Tomato", "Cabbage"
            };
        others = new ObservableCollection<string>
            {
                "Pizza", "Steak"
            };

        SelectionChangedCommand = new DelegateCommand<string>(item =>
            {
                // When user selects item in the listbox change the collection of your comboBox
                switch (item)
                {
                    case "Fruits":
                        {
                            Items = fruits;
                            break;
                        }
                    case "Vegetables":
                        {
                            Items = vegetables;
                            break;
                        }
                    case "Others":
                        {
                            Items = others;
                            break;
                        }
                }

                // Raise property change to make combobox update
                RaisePropertyChanged(() => Items);
            });               
    }

    // the collection currently displayed in the comboBox
    public ObservableCollection<string> Items { get; set; }

    // The selected item of the combobox
    public string CurrentItem { get; set; }

    public DelegateCommand<string> SelectionChangedCommand { get; set; }
}

ViewModel包含:

  1. 多个集合作为私人成员(水果蔬菜其他

  2. Items这是一个集合属性,根据ListBox中的选择表示ComboBox的当前项。

  3. CurrentItem属性绑定到SelectedItem的{​​{1}}。

  4. ComboBox在调用时,会根据给定参数(SelectionChangedCommand中的所选项目)更改集合。

  5. 请注意,此示例使用Prism框架,但您可以使用支持命令的任何MVVM framework来实现此功能。

    如果您没有使用任何框架,只需将“命令”中的代码视为每当ListBox的选择项发生更改时执行的代码(但您已实现该代码)。

    希望这有帮助