在DataGrid使用异步ItemsSource完成加载之后,该怎么办? [英] Do something after DataGrid finished loading with async ItemsSource?

查看:52
本文介绍了在DataGrid使用异步ItemsSource完成加载之后,该怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DataGrid ,可以加载大量项目,因此我将 ItemsSource 设置为 IsAsync = True .

I have a DataGrid that loads a large number of items, so I set the ItemsSource to IsAsync=True.

 <DataGrid Name="OrdersGrid" ItemsSource="{Binding Path=Orders, IsAsync=True}" />

除了在我的 UserControl 子类构造函数中更改 NewItemPlaceHolderPosition 之外,一切似乎都可以正常工作.

Everything seems to work fine except for changing the NewItemPlaceHolderPosition in my UserControl subclass constructor.

((IEditableCollectionView)OrdersGrid.Items).NewItemPlaceholderPosition = NewItemPlaceholderPosition.AtBeginning;

我认为这会崩溃,因为您无法将其设置为空网格,这是异步 ItemsSource 绑定之前的状态.

I assume that this crashes because you can't set it for an empty grid, which is what I have before the async ItemsSource binds.

因此,在尝试更改 NewItemPlaceholderPosition 之前,应在上面的行中确保已加载 DataGrid ?我需要类似"DataGridFinishedLoading"的内容,但我不知道可用的内容.

So where should I put the above line to make sure the DataGrid is loaded before I attempt to change the NewItemPlaceholderPosition? I need something like "DataGridFinishedLoading" but I don't know what is available.

推荐答案

Binding.NotifyOnTargetUpdated is what you are looking for.

在绑定和挂钩处理程序上将 NotifyOnTargetUpdated 设置为 true ,当目标(您的情况下为DataGrid)已更新.

Set NotifyOnTargetUpdated to true on your binding and hook handler which needs to be invoked when Target (DataGrid in your case) is updated.

您可以使用 args.Property 检查已通知了哪个绑定.

You can check with args.Property that which binding has been notified.

XAML

<DataGrid Name="OrdersGrid"
          ItemsSource="{Binding Path=Orders, IsAsync=True,
                                NotifyOnTargetUpdated=True}"
          TargetUpdated="DataGrid_TargetUpdated"/>

背后的代码

private void DataGrid_TargetUpdated(object sender, DataTransferEventArgs e)
{
    if (e.Property == DataGrid.ItemsSourceProperty)
    {
        ((IEditableCollectionView)OrdersGrid.Items).NewItemPlaceholderPosition = 
                                   NewItemPlaceholderPosition.AtBeginning;
    }
}

这篇关于在DataGrid使用异步ItemsSource完成加载之后,该怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆