如何使用自定义集合中的选定项

本文关键字:何使用 自定义 集合 | 更新日期: 2023-09-27 18:34:52

我有一个程序,我可以在其中更改comboBox中显示的项目集合。我之前在这个问题中已经谈到过它。无论如何,我现在正在使用自定义集合,因为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?)
        }
    }
}

如何为我创建并添加到comboBox的每个新集合分配一个值MySelectedItem?这样,每当我在comboBox切换到不同的集合时,都会显示selectedItem

更新

我的收藏现在设置为ObservableCollection<string>

适用于ListBoxComboBox的 XAML

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

**ListBox下的新SelectedCommand物业:

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我一定忘记了,或者不明白了什么。

如何使用自定义集合中的选定项

不需要

自定义集合即可绑定所选项。下面是如何实现此目的的示例:

   <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,每当列表中的选择发生更改时都会调用该命令。

这是视图模型

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

视图模型包含:

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

  2. Items 它是一个集合属性,表示基于列表框中的选择的ComboBox的当前项。

  3. CurrentItem财产绑定到ComboBoxSelectedItem

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

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

如果您没有使用任何框架,只需将"命令"中的代码视为每当更改ListBox的选择项时执行的代码(但是您已经实现了它(。

希望这有帮助