如何在wpf mvvm中点击按钮时将集合绑定到xamdatagrid?
答案 0 :(得分:3)
XAML
<Button Command="{Binding ButtonCommand}">Button</Button>
<DataGrid ItemsSource="{Binding MyCollection}">
...
configure columns to MyRowViewModel
...
</DataGrid>
视图模型
public class MyViewModel
{
public ICommand ButtonCommand {get;set;}
public ObservableCollection<MyRowViewModel> MyCollection {get;set;}
}
并在OnLoad中设置:
View.DataContext = viewModel;
当调用命令时,更改您的集合或创建并自动更新网格wpf