Question

How to bind a collection to xamdatagrid on button click in wpf mvvm?

Was it helpful?

Solution

XAML

   <Button Command="{Binding ButtonCommand}">Button</Button>
   <DataGrid ItemsSource="{Binding MyCollection}">
   ...
   configure columns to MyRowViewModel
   ...
   </DataGrid>

ViewModel

   public class MyViewModel
   {
     public ICommand ButtonCommand {get;set;}
     public ObservableCollection<MyRowViewModel> MyCollection {get;set;}
   }

and set in OnLoad:

   View.DataContext = viewModel;

and when command is called change your collection or create and wpf update your grid automatically

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top