DataGrid 更新的 WPF 事件

WPF event for DataGrid update

我有一个带有数据网格的视图:

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

我还有 10 个按钮,每次单击一个按钮时,我都会调用更新 DataSourceTable 的命令(在我的 ViewModel 中是一个数据表 属性)。
代码运行良好,每次我点击一个按钮时,我的数据网格都有相应的数据。

我的问题是找不到每次更改 DataSourceTable 时都会引发的数据网格事件。

PS。我尝试了 DARViewer.DataContextChanged(这是有道理的)和 DARViewer.SourceUpdated 但它们不起作用

按钮执行的命令是:

  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

您可以使用 ItemsSourceChangedEvent。

有关详细信息,请查看此处提供的答案:

How to raise an event when DataGrid.ItemsSource is changed

在 Marcus 提供的解决方案之后,我将 post 使用 VB.NET 代码的相同解决方案,以防万一有人需要它。

 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