【问题标题】:How to let MainWindow know that in Folder/Page2.xaml a Button is clicked如何让 MainWindow 知道在 Folder/Page2.xaml 中单击了一个按钮
【发布时间】:2017-07-05 18:44:40
【问题描述】:

在我的 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 并且 Main.Content 必须更改为

Main.Content = new Privatecustomer.privatecustomer_add();

? 我希望我能在这里得到一些帮助

【问题讨论】:

  • 在 privatecustomers_show.xaml 中:public event EventHandler ButtonFirstFormClicked; public void addPrivatkunde(object sender, RoutedEventArgs e) { if (ButtonFirstFormClicked != null) ButtonFirstFormClicked(sender, e); } 在 MainWindow.xaml 中:Privatecustomer.privatecustomer_show.ButtonFirstFormClicked += (s, e) { Main.Content = new Privatecustomer.privatecustomer_add(); } 但这不起作用,因为 MainWindow 不知道 Privatecustomer.privatecustomer_show.ButtonFirstFormClicked += (s, e)
  • 每次点击是处理同一个页面还是创建一个新页面?
  • 你是什么意思?我有我的 MainWindow.xaml。有一个框架显示我想查看的页面。如果我单击 MainWindow 上的“私人客户”按钮。框架向我显示了来自 privatecustomers_show.xaml 的内容。这行得通。但是当我单击 privatecustomers_show 中的按钮时,我希望 MainWindow 中的 Frame.Content 更改为“Main.Content = new privatecustomer_add”。

标签: c# wpf xaml


【解决方案1】:

将Page2.Tag 属性设置为MainWindow 的实例(在Xaml 中使用绑定,或者简单地在代码中设置)。单击 Page2 中的指定 Button 后,只需使用 Tag 属性(现在是您的 MainWindow)。

另一种选择是使用

App.Current.MainWindow

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

【讨论】:

  • 我不这么认为。我没有多个窗口。我有 1 个窗口(MainWindow.xaml),在这个 MainWindow 中是一个框架,其中包括单击按钮的页面
【解决方案2】:

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

像这样

  public event EventHandler AddPrivateCustomer;

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

更新:请注意更新的代码,我在第一个版本中犯了复制粘贴错误。

改变你的: private void privatecustomer_Click(object sender, RoutedEventArgs e)

收件人:

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 将订阅每个事件。

【讨论】:

  • 我不明白这个。如果我把你的代码放在 privatecustomer_show.xaml.cs 中,Visual Studio 会给我命名空间错误,因为他不知道“AddPrivateCustomerEventHandler”
  • 查看我更新的代码,我在第一个版本中出现了编码错误。
  • 您可以通过更改应用程序的设计使其更简单。而不是在 showcustomer 控件中添加按钮,而是在 MainWindow 中显示它。 ----------------------- MainWindow -------- ShowCustomer ------- |客户名单 | ------------------------------------------------ [添加客户] [删除客户] 这有意义吗?
  • i.imgur.com/ZViIqIZ.png希望你能看懂这张图。这就是我要的。你的protected virtual void OnAddPrivateCustomerEventArgs e) { if (AddPrivateCustomer!= null) AddPrivateCustomer(this, e); } 不起作用:/
  • 我尝试将protected virtual void OnAddPrivateCustomerEventArgs e) 更改为protected virtual void OnAddPrivateCustomer(EventArgs e) 但还是不行
猜你喜欢
  • 2021-05-17
  • 2017-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多