如何在 wpf 数据绑定发生时显示加载图形/动画

发布于 2024-08-18 08:46:48 字数 305 浏览 2 评论 0原文

我有一个包含 DataGrid 的 WPF 用户控件。我将视图模型的 ObservableCollection 绑定到它。每个视图模型都有另一个视图模型集合,我用它们来绑定另一个 DataGrid 。因此,效果是一个 DataGrid,其中包含行详细信息模板中的嵌套 DataGrid

通常,绑定速度非常快,但有时当有大量数据时,它可能会在绑定/绘图发生时挂起 UI。

有没有一种方法可以在绑定/绘图正在进行时显示加载动画或进度条?

I have a WPF user control that contains a DataGrid. I'm binding an ObservableCollection of view models to it. Each view model has another collection of view models that I'm using to bind another DataGrid to. So the effect is a DataGrid with a nested DataGrid contained in the row details template.

Normally the binding is quite quick, but sometimes when there's a lot of data it can hang the UI while the binding/drawing is taking place.

Is there a way where I can either show a loading animation or progress bar while the binding/drawing is in progress?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

牛↙奶布丁 2024-08-25 08:46:49

我有同样的问题,这就是我解决它的方法。

我发现 DataGrid 仅在显示网格时才开始创建控件。就我而言,这是一个耗时的过程。经过一番跟踪后,我发现创建控件是在测量期间发生的!

我的解决方案是重写 MeasureOverride 并将等待光标放在基类调用周围。我将等待光标设置封装在一个类中。所以代码看起来像这样。

    protected override Size MeasureOverride(Size availableSize)
    {
        using (new DisposableWaitCursor(this))
        {
            return base.MeasureOverride(availableSize);
        }
    }

I had the same problem and this is how I solved it.

I discovered that DataGrid will only start creating controls when it displays the grid. In my case this was the time consuming process. After some tracing I found that creating the controls happens during measuring !

My solution is to override MeasureOverride and put the wait cursor around the base class call. I encapsulated my wait cursor setting in a class. So the code looks like this.

    protected override Size MeasureOverride(Size availableSize)
    {
        using (new DisposableWaitCursor(this))
        {
            return base.MeasureOverride(availableSize);
        }
    }
淡淡離愁欲言轉身 2024-08-25 08:46:48

可能有一个更正式的,或者至少更简单的解决方案,但是您可以使用模式弹出窗口,该窗口显示在工作线程中,并在网格完成加载时异步关闭:

Window waitWindow = new Window { Height = 100, Width = 200, WindowStartupLocation = WindowStartupLocation.CenterScreen, WindowStyle = WindowStyle.None };
waitWindow.Content = new TextBlock { Text = "Please Wait", FontSize = 30, FontWeight = FontWeights.Bold, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center };

BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += delegate
{
    Dispatcher.BeginInvoke(new Action(delegate { waitWindow.ShowDialog(); }));

    DataLoader dataLoader = new DataLoader(); // I made this class up
    dataLoader.DataLoaded += delegate
    {
        Dispatcher.BeginInvoke(new Action(delegate() { waitWindow.Close(); }));
    };

    dataLoader.LoadData();
};

worker.RunWorkerAsync();

您可以用类似加载栏的东西替换 TextBlock ,并且您可以通过参数化处理网格加载的对象并将其传递给常用方法来使代码可重用。

我希望这对你有用。

There's probably a more formal, or at least simpler solution, but you could use a modal popup window that is shown in a worker thread and is closed asynchronously when your is grid done loading:

Window waitWindow = new Window { Height = 100, Width = 200, WindowStartupLocation = WindowStartupLocation.CenterScreen, WindowStyle = WindowStyle.None };
waitWindow.Content = new TextBlock { Text = "Please Wait", FontSize = 30, FontWeight = FontWeights.Bold, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center };

BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += delegate
{
    Dispatcher.BeginInvoke(new Action(delegate { waitWindow.ShowDialog(); }));

    DataLoader dataLoader = new DataLoader(); // I made this class up
    dataLoader.DataLoaded += delegate
    {
        Dispatcher.BeginInvoke(new Action(delegate() { waitWindow.Close(); }));
    };

    dataLoader.LoadData();
};

worker.RunWorkerAsync();

You can replace the TextBlock with something pretty like a loading bar, and you could make the code re-usable by parameterizing the object that handles the loading of the grid(s) and passing it in to a commonly used method.

I hope that works for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文