0

C# WPF我有一个窗口,它使用this.WorkingFrame.Navigate(Page1); 第 1 页上的“现在”托管一个页面,我有一个listview. 在第 1 页上,您可以双击listview打开新窗口 (Page2) 中的项目来编辑该项目。一旦项目被编辑,它被保存到datacontext. 现在我遇到的问题是,一旦 Page2 关闭,listview就不会更新。基本上我必须离开页面并返回它以显示更改。有没有办法从 Page2 刷新 Page1 以显示所做的更改?这是我的代码

public partial class Page1 : Page
{
    public Page1()
    {            
        InitializeComponent();
    }
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        //initiates the method to load data into the list view
        LoadLV();
    }
    //Loads data into the list view object
    public void LoadLV()
    {

        auroraDataEntities = new AuroraDataEntities();
        Data data = new Data();
        ObjectQuery<Inventory> inventories = auroraDataEntities.Inventories;
        //Returns only objects with a quantity greater than 0, so it won't show anything you are out of
        var fillList = from q in inventories
                       where q.Qty > 0
                       select q;
        ingListLV.ItemsSource = fillList.ToList();

     }
    //Method to open what I want to be the child window basically a popup Dialog box
    private void Open_Page2(object sender, RoutedEventArgs e)
    {
        Page2 openPage2 = new Page2();
        openPage2.Show();

    }
}

    //This is the code for Page 2
public partial class Page2 : Window
{    


     public Page2()
    {            
        InitializeComponent();
        //ADDED a reference to Page1 in the constructor
        Page1 page1;
    }

    //Method when i click the close button on the page
    private void Close_Button(object sender, RoutedEventArgs e)
    {
    //In here is the code that I want to use to refresh the listview on page 1
            //ADDED the call to the public method LoadLV on page 1
            page1.LoadLV()              
    }
}
4

2 回答 2

1

这个问题更加普遍 - 视图可能会任意复杂,并且在某处修改的数据应该在其他地方失效/刷新。假设您不仅有两个处于父子关系的窗口,而且还有一个复杂的嵌套视图,带有选项卡或动态浮动窗口。显然,这不仅仅是“从孩子刷新父母”。

那么有什么可能的方法呢?答案是:消息传递发布-订阅模式。您需要一个Event Aggregator,例如在 Prism4 中有一个。聚合器让您的视图订阅消息并发布消息。

您的情况下的消息传递架构很简单。你需要一个事件,比如

public class BusinessEntityUpdatedEvent : CompositePresentationEvent
{
   public object Entity { get; set; }

   //or

   // if your entities share a common base class
   public EntityBase Entity { get; set; }   

   //or

   public justAnything ofAnyType { get; set; }
   public orEvenMore ofAnotherType { get; set; }
}

然后您的父视图订阅该事件,您的子视图发布事件,在事件参数中传递更新的实体。

一旦你有了在应用程序的不同区域之间传递消息的想法,你的代码就会变得不那么耦合,你就会停止思考“这两个类之间是否存在联系?也许是父子关系?” 而是您开始思考“应该从那里流向那里的信息是什么?”。

编辑一个短期的解决方案是:

public class Page1
{ 
   private void OpenPage2()
   {
      Page2 p = new Page2( this );
      p.Show();
   }

   public void LoadLv() ...
}

public class Page2
{
   private Page1 page1;
   public Page2( Page1 page1 )
   {
      this.page1 = page1;
   }

   public void CloseButton()
   {
      this.page1.LoadLV();
   }
}
于 2013-08-10T17:01:54.573 回答
0
MainWindow mainWindow = new MainWindow();

mainWindow.Closed += (s, eventarg) =>
{
    // Your method to update parent window
};

mainWindow.Show();
于 2021-04-03T10:06:57.087 回答