简体   繁体   中英

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 .

I would like the NewItemPlaceholderPosition to be AtBeginning for both grids. This is easy enough for OrdersGrid because I can just set it in my UserControl subclass constructor:

((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 .

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 .

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 .

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. Please help!

I was able to solve this problem by adding the following to my UserControl subclass constructor:

((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:

private void DetailsSourceChanged(object sender, EventArgs e)
{
    if (DetailsGrid.Items.Count > 0)
    {
        ((IEditableCollectionView)DetailsGrid.Items).NewItemPlaceholderPosition = NewItemPlaceholderPosition.AtBeginning;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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