[英]DataGrid NewItemPlaceholderPosition lost when ItemsSource changed
I have two DataGrids above one another, and selecting a row in the top grid, OrdersGrid
, displays some details in the bottom grid, DetailsGrid
, about the selected row from OrdersGrid
. 我上面有两个DataGrid,然后在顶部网格
OrdersGrid
选择一行,在底部网格OrdersGrid
中显示一些有关DetailsGrid
所选行的OrdersGrid
。
I would like the NewItemPlaceholderPosition
to be AtBeginning
for both grids. 我想
NewItemPlaceholderPosition
被AtBeginning
两个网格。 This is easy enough for OrdersGrid
because I can just set it in my UserControl
subclass constructor: 这对于
OrdersGrid
很容易,因为我可以在UserControl
子类构造函数中进行设置:
((IEditableCollectionView)OrdersGrid.Items).NewItemPlaceholderPosition = NewItemPlaceholderPosition.AtBeginning;
But the problem is that the ItemsSource
for DetailsGrid
is a member of the currently selected item in OrdersGrid
. 但是问题在于
DetailsGrid
的ItemsSource
是DetailsGrid
中当前选定项目的OrdersGrid
。
If I set NewItemPlaceholderPosition
for DetailsGrid
as above then it works until I click on a new row in OrdersGrid
when it goes back to the default of being at the bottom since DetailsGrid
reloads from its new ItemsSource
. 如果我设置
NewItemPlaceholderPosition
为DetailsGrid
如上然后它工作,直到我在一个新行单击OrdersGrid
时,它可以追溯到的是在底部的默认,因为DetailsGrid
从它的新的重新加载ItemsSource
。
The ItemsSource
for OrdersGrid
is an ObservableCollection
called Orders
, which contains Objects of type Order
, and the ItemsSource
for DetailsGrid
is Order.Details
, also an ObservableCollection
, for the current Order
. OrdersGrid
的ItemsSource
是一个名为Orders
的ObservableCollection
,其中包含Order
类型的对象, DetailsGrid
的ItemsSource
是Order.Details
,也是当前Order
的ObservableCollection
。
I'm thinking I want something like an ItemsSourceChanged
event for DetailsGrid
, but I'm not sure if this is the correct approach or even how to go about this. 我以为我想要类似
DetailsGrid
的ItemsSourceChanged
事件,但是我不确定这是否是正确的方法,甚至如何解决。 Please help! 请帮忙!
I was able to solve this problem by adding the following to my UserControl
subclass constructor: 通过将以下内容添加到我的
UserControl
子类构造函数中,我能够解决此问题:
((IEditableCollectionView)DetailsGrid.Items).NewItemPlaceholderPosition = NewItemPlaceholderPosition.AtBeginning;
var dpd = DependencyPropertyDescriptor.FromProperty(ItemsControl.ItemsSourceProperty, typeof(DataGrid));
if (dpd != null)
{
dpd.AddValueChanged(DetailsGrid, DetailsSourceChanged);
}
with the DetailsSourceChanged
method defined as followed: 使用
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.