0

に表示されるアイテムのコレクションを変更するプログラムがあります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?)
        }
    }
}

MySelectedItem作成して に追加する新しいコレクションごとに値を割り当てるにはどうすればよいcomboBoxですか? comboBoxこれにより、で別のコレクションに切り替えるたびにselectedItemが表示されるようになります。

アップデート

私のコレクションは現在 として設定されていObservableCollection<string>ます。

ListBoxとの XAMLComboBox

<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プログラムは、コレクションごとに選択された をまだ保持していません。私は何かを忘れているか、理解していないに違いありません。

4

1 に答える 1