How to change the content in a datagrid or listview using MVVM
I have a MainWindowViewModel (DataContext) which exposes different ObservableCollections<> (viewmodels o开发者_开发百科f the poco objects with INotifyProperty.... ).After selecting a value from a combobox i want to click a load button which then displays the desired data in a datagrid or listview. Problem is, that in the xaml code i can only define the itemssource and column specific definitions for one collection. So how can i change the data that is displayed in way that is compliant with MVVM. Are there any best practices?
Create in your ViewModel a property that exposes an IEnumerable
. Bind the DataGrids ItemsSource
to that property. You can set then the property from inside the ViewModel to any of your collections and then the data will change.
Please note that you should specify two-way binding and that your property must support some change-notification (support INotifyPropertyChanged
or be a DependencyProperty
).
精彩评论