![](/img/trans.png)
[英]Why does the DataGrid not update when the ItemsSource is changed?
[英]DataGrid NewItemPlaceholderPosition lost when ItemsSource changed
我上面有两个DataGrid,然后在顶部网格OrdersGrid
选择一行,在底部网格OrdersGrid
中显示一些有关DetailsGrid
所选行的OrdersGrid
。
我想NewItemPlaceholderPosition
被AtBeginning
两个网格。 这对于OrdersGrid
很容易,因为我可以在UserControl
子类构造函数中进行设置:
((IEditableCollectionView)OrdersGrid.Items).NewItemPlaceholderPosition = NewItemPlaceholderPosition.AtBeginning;
但是问题在于DetailsGrid
的ItemsSource
是DetailsGrid
中当前选定项目的OrdersGrid
。
如果我设置NewItemPlaceholderPosition
为DetailsGrid
如上然后它工作,直到我在一个新行单击OrdersGrid
时,它可以追溯到的是在底部的默认,因为DetailsGrid
从它的新的重新加载ItemsSource
。
OrdersGrid
的ItemsSource
是一个名为Orders
的ObservableCollection
,其中包含Order
类型的对象, DetailsGrid
的ItemsSource
是Order.Details
,也是当前Order
的ObservableCollection
。
我以为我想要类似DetailsGrid
的ItemsSourceChanged
事件,但是我不确定这是否是正确的方法,甚至如何解决。 请帮忙!
通过将以下内容添加到我的UserControl
子类构造函数中,我能够解决此问题:
((IEditableCollectionView)DetailsGrid.Items).NewItemPlaceholderPosition = NewItemPlaceholderPosition.AtBeginning;
var dpd = DependencyPropertyDescriptor.FromProperty(ItemsControl.ItemsSourceProperty, typeof(DataGrid));
if (dpd != null)
{
dpd.AddValueChanged(DetailsGrid, DetailsSourceChanged);
}
使用DetailsSourceChanged
方法定义如下:
private void DetailsSourceChanged(object sender, EventArgs e)
{
if (DetailsGrid.Items.Count > 0)
{
((IEditableCollectionView)DetailsGrid.Items).NewItemPlaceholderPosition = NewItemPlaceholderPosition.AtBeginning;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.