DataGrid 的源正在更新时会触发哪个事件?我已经尝试过 DataContextChanged 和 SourceUpdated 了,但都没有生效。
实际上我只需要一个简单的功能。如果有新行添加,让 GridView 的滚动条自动滚动到底部以便查看新增的内容。
DataGrid 的源正在更新时会触发哪个事件?我已经尝试过 DataContextChanged 和 SourceUpdated 了,但都没有生效。
实际上我只需要一个简单的功能。如果有新行添加,让 GridView 的滚动条自动滚动到底部以便查看新增的内容。
我曾经遇到同样的问题,我是这样解决的
DataGrid myGrid = new DataGrid();
CollectionView myCollectionView = (CollectionView)CollectionViewSource.GetDefaultView(myGrid.Items);
((INotifyCollectionChanged)myCollectionView).CollectionChanged += new NotifyCollectionChangedEventHandler(DataGrid_CollectionChanged);
然后您需要在事件处理程序 DataGrid_CollectionChanged
中实现逻辑。
将NotifyOnTargetUpdated属性设置为true,用于ItemsSource绑定,并处理TargetUpdated事件。如果您有多个绑定,请查找DataTransferEventArgs属性以确定目标是否为ItemsSource。
就我而言,我使用了SelectionChange通知,它会触发每个事件Del/Add/Edit/Select。它的效果非常好。
private void dataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Console.WriteLine("hi");
}