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