如何从子窗口MVVM刷新datagrid [英] how to refresh datagrid from a child window MVVM

查看:73
本文介绍了如何从子窗口MVVM刷新datagrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在关闭子窗口时刷新数据网格。但它不起作用,请检查代码。我调试了代码,我可以看到它可以在子调用刷新函数时从父窗口获取数据。

Hi, I am trying to refresh a datagrid when closing a child window. But it doesnt work, please check the code. I debugged the code, and I can see it is able to get the data from the parent window when child call the refresh function.

子窗口关闭方法。

 foreach (System.Windows.Window window in System.Windows.Application.Current.Windows)
                        {
                            if (window.DataContext == this)
                            {
                                window.Close();
                                _viewmodel = new MainWindowViewModel();
                                _viewmodel.LoadPathData();
                            }
                        }

在父窗口中加载数据函数

load data function in parent window

 public void LoadPathData()
        {
            _ctx = new CourtFLOWEntities();
            ComboCollection.Source = _ctx.EncryptionStorages.Select(x => x).ToList();
        }

推荐答案

您正在创建MainWindowViewModel类的新实例并调用其LoadPathData()...这没有多大意义。您希望看到任何东西如何以及在哪里得到更新?您要刷新的父窗口的引用在哪里?

You are creating a new instance of the MainWindowViewModel class and call its LoadPathData()...this doesn't make much sense. How and where are you expecting to see anything get refreshed? Where is the reference to the parent window that you want to refresh?

您可能希望将Application.Current.Windows中窗口的DataContext强制转换为MainWindowViewModel并调用LoadPathData( )此实例的方法:

You probably want to cast the DataContext of a window in the Application.Current.Windows to a MainWindowViewModel and call the LoadPathData() method of this instance:

foreach (System.Windows.Window window in System.Windows.Application.Current.Windows)
            {
                if (window.DataContext == this)
                {
                    window.Close();
                    continue;
                }

                MainWindowViewModel viewmodel = window.DataContext as MainWindowViewModel;
                if(viewmodel != null)
                    viewmodel.LoadPathData();
            }

希望有所帮助。

请记得关闭你的帖子通过标记有用的帖子作为答案,然后如果你有一个新问题就开始一个新的主题。请不要在同一个帖子中提出几个问题。

Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.


这篇关于如何从子窗口MVVM刷新datagrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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