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