简体   繁体   English

ItemsSource更改时,DataGrid NewItemPlaceholderPosition丢失

[英]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. 我想NewItemPlaceholderPositionAtBeginning两个网格。 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 . 但是问题在于DetailsGridItemsSourceDetailsGrid中当前选定项目的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 . 如果我设置NewItemPlaceholderPositionDetailsGrid如上然后它工作,直到我在一个新行单击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 . OrdersGridItemsSource是一个名为OrdersObservableCollection ,其中包含Order类型的对象, DetailsGridItemsSourceOrder.Details ,也是当前OrderObservableCollection

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. 我以为我想要类似DetailsGridItemsSourceChanged事件,但是我不确定这是否是正确的方法,甚至如何解决。 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM