我有一个没有MVVM的WPF应用程序,我决定将它重构为MVVM。我遇到了一个ComboBox SelectionChanged事件的问题。为了简单起见,让我们假设我有一个ComboBox和2个ListView,每个ComboBoxItem都是一个集合。首先,ListView的ItemsSource绑定到来自ComboBox.SelectedValue的集合,但仅绑定到(十进制的)一个属性大于零的部分。第二,ListView的ItemsSource绑定到相同的对齐,但绑定到第二部分(一些支柱大于零)。下面是一些需要理解的代码
private void myCombo_selectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox myCmb = sender as ComboBox;
List<myType> myList = myCmb.SelectedValue as List<myType>;
itemsForListView1 = myList.Where((x) => x.myProp > 0);
itemsForListView2 = myList.Where((x) => x.myProp < 0);
// Above 2 collections are of Type List<myType> and their scope will be whole ViewModel,
//so i assume i just need to change them and RaisePropChanged but how to change them without breaking mvvm ?
MyListView1.ItemsSource = itemsForListView1;
MyListView2.ItemsSource = itemsForListView2;
}
如何在MVVM中实现类似的功能?
发布于 2015-04-17 11:47:19
我建议了一个解决方案,如下面的伪代码。将SelectedItem与myCombo的SelectedValue绑定,ItemsListViewX与ListView绑定
private List<T> selectedItem;
public List<T> SelectedItem
{
get { return selectedItem; }
set
{
if (value != selectedItem)
{
selectedItem = value;
ItemsListView1 = new ObservableCollection<T>(selectedItem.Where(x=>x.Prop>0));
ItemsListView2 = new ObservableCollection<T>(selectedItem.Where(x=>x.Prop<0));
NotifyOfPropertyChange(() => SelectedItem);
}
}
}
private ObservableCollection<T> itemsListView1;
public ObservableCollection<T> ItemsListView1
{
get { return itemsListView1; }
set
{
if (value != itemsListView1)
{
itemsListView1 = value;
NotifyOfPropertyChange(() => ItemsListView1);
}
}
}
private ObservableCollection<T> itemsListView2;
public ObservableCollection<T> ItemsListView2
{
get { return itemsListView2; }
set
{
if (value != itemsListView2)
{
itemsListView2 = value;
NotifyOfPropertyChange(() => ItemsListView2);
}
}
}
也许你也对MVVM框架感兴趣。它将加强您的数据库。
发布于 2015-04-17 11:56:08
如果我正确理解了,您想要的是处理SelectionChanged
中的ViewModel,并对itemsForListView1
和itemsForListView2
进行一些更改,而不是对您目前正在查看的内容进行更改?
1)在ViewModel中,您需要:创建一个ICommand
公共属性,该属性可以作为DelegateCommand
从Microsoft.Practices.Composite.Presentation.Commands
实例化
...
public ViewModelConstructor(...)
{
...
SelectionChangedCommand = new DelegateCommand<List<myType>>(SelectionChangedExecute);
...
}
...
public List<myType> ItemsForListView1 {get; private set} // Implement INotifyPropertyChanged here
public List<myType> ItemsForListView1 {get; private set} // Implement INotifyPropertyChanged here
...
public ICommand SelectionChangedCommand { get; private set; }
private void SelectionChangedExecute(List<myType> parameter)
{
ItemsForListView1 = parameter.Where((x) => x.myProp > 0).ToList();
ItemsForListView2 = parameter.Where((x) => x.myProp < 0).ToList();
}
...
2)在视图中,您希望将Loaded和SelectionChanged绑定到视图模型中新创建的命令,并且希望将ListView绑定到viewmodel中的集合
<ComboBox x:Name="MyComboBox" SelectedIndex="0" ItemsSource="{Binding YourCollectionWithDifferentLists}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<i:InvokeCommandAction Command="{Binding SelectionChangedCommand}" CommandParameter="{Binding ElementName=Combo, Path=SelectedItem}"></i:InvokeCommandAction>
</i:EventTrigger>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding SelectionChangedCommand}" CommandParameter="{Binding ElementName=Combo, Path=SelectedItem}"></i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
如果集合正在更改,并且希望在UI上显示更改,则可能需要使用ObservableCollection而不是List。如果要在选择更改事件之后创建一个新集合,则需要以任何方式实现INotifyPropertyChanged。
https://stackoverflow.com/questions/29697972
复制相似问题