1

I have a Datagrid. I want to refresh it when a event occurs.

So I have this :

 <DataGrid Name="matrix"  
Background="#3f4953" 
dataGrid2D:ItemsSource.Array2D="{Binding Path= Matrix,  
Mode=TwoWay, 
UpdateSourceTrigger=PropertyChanged
}"/>

I made this property in my ViewModel :

public string[,] Matrix { get; set; }

I made some maths to populate a 2d Array and I have this :

 Matrix = resultOfMyMaths;

I have this in my Xaml too :

  <UserControl.DataContext>
        <Binding Source="{StaticResource vm}" />
    </UserControl.DataContext>

How can i Refresh my Datagrid now ?

For information : I change the value of Matrix in my code, it works. But my gridView doesn't refreh. I just have to bind the new value of Matrix in my datagrid.

I can't use an observableCollection because it's a multidimentional Array. And I can't cast my array to ObservableCollection. I already tried.

Ugur
  • 1,257
  • 2
  • 20
  • 30
  • I guess, u should define a ObservableCollection of Matrix. So like ObservableCollection. Then use binding or define a custom Matrix class to get regarding properties – Ugur Jul 19 '19 at 12:13
  • Implement `INotifyPropertyChanged` (your xaml is already looking for that event! `UpdateSourceTrigger=PropertyChanged`). Then once you want to update the property you would call OnPropertyChanged([CallerMemberName]string property = null). Ideally you would use an ICommand if you update through Button click, if not then just call OnPropertyChancged. – XAMlMAX Jul 19 '19 at 12:49

1 Answers1

0

You could implement INotifyPropertyChanged interface on ViewModel class and fire PropertyChanged event with sender set to this and event args to new PropertyCahngedEventArgs("Matrix") to refresh binding.

Further reading on MSDN about INotifyPropertyChanged: https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.inotifypropertychanged?view=netframework-4.8

Misaz
  • 3,694
  • 2
  • 26
  • 42