如何让MainWindow知道在文件夹/page2.xaml中单击一个按钮



在我的mainwindow.xaml中,我有一个框架。当我单击按钮时,此框架的内容将更改。因此,如果我单击一个按钮显示私人客户,则框架内容显示了私人客户的网站:

private void privatecustomer_Click(object sender, RoutedEventArgs e)
    {
        Main.Content = new Privatecustomer.privatecustomer_show();
    }

单击此按钮后,帧将更改内容。新内容显示privatecustomer.privatecustomer_show.xaml现在。在此XAML中,我有另一个按钮。如果我单击此按钮,MainWindow中框架的内容应更改为" PrivateCustomer.privatecustomer_add.xaml"。

但是我如何从privatecustomer_show.xaml到mainwindow.xaml,单击privatecustomer_show中的按钮addprivatecustomer,单击了aiddprivatecustomer,并且the main.content必须更改为

Main.Content = new Privatecustomer.privatecustomer_add();

?我希望我可以在这里获得一些帮助

如何在mainwindow.xaml可以订阅的privatecustomer_show.xaml中添加事件。

喜欢这个

  public event EventHandler AddPrivateCustomer;
  protected virtual void OnAddPrivateCustomerEventArgs e) 
  {
     if (AddPrivateCustomer!= null)
        AddPrivateCustomer(this, e);
  }

更新:请注意更新的代码,我在第一个版本中犯了一个copy'n'paste错误。

更改您的: 私有void privatecustomer_click(对象发送者,routedeventargs e)

to:

private void privatecustomer_Click(object sender, RoutedEventArgs e)
{
  var privateCustomerContent=new Privatecustomer.privatecustomer_show();
  privateCustomerContent.AddPrivateCustomer+=onClick_addPrivateCustomer;
  Main.Content = privateCustomerContent;
}
private onClick_addPrivateCustomer(object o,EventArgs e)
{
  // Change Main.Content
}

这个想法是,您的privatecustomer_show控件发送事件,以便何时要更改其访问权限之外的事件。您可以完全控制其所有子窗口的MainWindow,然后订阅每个事件。

Page2.Tag属性设置为MainWindow的实例(在XAML中使用绑定,或简单地将其设置为代码)。Page2中指定的按钮是单击之后,只需使用标签属性(现在是您的MainWindow)。

另一个选择是使用

App.Current.MainWindow

单击按钮时。(警告。我不知道您的项目的结构,这可能不是您要寻找的实例)。

最新更新