2

如何在wpf mvvm中单击按钮时将集合绑定到xamdatagrid?

4

1 回答 1

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 自动更新您的网格

于 2012-09-13T10:47:36.120 回答