How to change the content in a datagrid or listvie

2019-07-18 10:23发布

问题:

I have a MainWindowViewModel (DataContext) which exposes different ObservableCollections<> (viewmodels of 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?

回答1:

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).



标签: .net wpf mvvm