我有一个程序,我可以在其中更改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>
.
XAMLListBox
和ComboBox
<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
为每个集合选择的那个。我一定是忘记了,或者不明白什么。