我在WPF中沒有MVVM的應用程序,我決定將此重構爲MVVM。我遇到了ComboBox SelectionChanged事件的問題。基本上,讓簡單讓我們假設我有一個組合框和2個ListView的。每個ComboBoxItem都是一個集合。第一個ListView的ItemsSource綁定到來自ComboBox.SelectedValue的集合,但僅限於其一個屬性(十進制)大於零的部分。第二個ListView的ItemsSource綁定到相同的集合,但是到第二個部分(一些prop大於零)。下面一些代碼要了解MVVM中的ComboBox SelectionChanged
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中類似的東西?
將此事件方法設置爲屬性,該屬性可以自我提升並使用其他屬性與列表視圖進行綁定。 – JSJ
我對MVVM真的很陌生,所以我會用一些進一步的描述來欣賞一些exmaple – MajkeloDev