0

I have a View with a dataGrid:

<DataGrid x:Name="DARViewer"
                      AutoGenerateColumns="True"
                      ItemsSource="{Binding DataSourceTable,
                                            NotifyOnSourceUpdated=True,
                                            UpdateSourceTrigger=Default}" />

I have also 10 buttons and every time that I click on one I call a command that updates the DataSourceTable (is a datatable property in my ViewModel).
The code is working fine, and every time I click on a button my datagrid has the corresponding data.

My problem is that I cannot find an datagrid event that is raised every time the DataSourceTable is changed.

PS. I tried DARViewer.DataContextChanged(that makes sense) and DARViewer.SourceUpdated but they are not working

The command that is executed by the buttons is:

  Private Sub LoadReportExecute(sender)
    Id = sender.DataContext.Id
    ChangeDataSourceTable()
  End Sub

 Public Sub ChangeDataSourceTable()
    DataSourceTable = Nothing
    DataSourceTable = ExecuteCommand(DataSource) 'that just returns a datatable
 End Sub

 Private mdtDataSourceTable As  DataTable
 Public Property DataSourceTable() As DataTable
    Get
        Return mdtDataSourceTable
    End Get
    Set(ByVal value As DataTable)
        mdtDataSourceTable = value
        RaisePropertyChanged("DataSourceTable")
    End Set
 End Property
Nianios
  • 1,391
  • 3
  • 20
  • 45
  • Can we see the command called on your button clicks? – Kestami Mar 27 '15 at 09:01
  • @Shane.C I edited my question to include more code – Nianios Mar 27 '15 at 09:12
  • I'm trying to figure out how you'd write what i want in vb.net, i think i know how to do it in c#. You can build a CollectionView from DARViewer.items, and attach INotifyCollectionChanged. In C# it would look like this.. CollectionView myCollectionView = (CollectionView)CollectionViewSource.GetDefaultView(myGrid.Items); ((INotifyCollectionChanged)myCollectionView).CollectionChanged += new NotifyCollectionChangedEventHandler(DataGrid_CollectionChanged); – Kestami Mar 27 '15 at 09:47

2 Answers2

0

You could use ItemsSourceChangedEvent.

For more information look at the answer provided here:

How to raise an event when DataGrid.ItemsSource is changed

Community
  • 1
  • 1
MarkusE
  • 545
  • 4
  • 20
0

After the solution that Marcus provided I'll post the same solution with code for VB.NET just in case someone needs it.

 Private Sub ReportView_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
    Dim gridItemsSourceDescriptor As DependencyPropertyDescriptor = DependencyPropertyDescriptor.FromProperty(DataGrid.ItemsSourceProperty, GetType(DataGrid))
    gridItemsSourceDescriptor.AddValueChanged(Me.DARViewer, _
                                               New EventHandler(AddressOf OnDataGridItemsSourceChanged))
End Sub


Private Sub OnDataGridItemsSourceChanged(sender As Object, e As EventArgs)
    'Code here
End Sub
Nianios
  • 1,391
  • 3
  • 20
  • 45