如何让MainWindow知道在Folder / Page2.xaml中单击了一个Button

时间:2023-01-27 15:56:25

In my MainWindow.xaml i have a Frame. The content of this Frame will change when i click a Button. So if I click a button to show private customers, the Frame Content shows the site of the private customers:

在我的MainWindow.xaml中,我有一个框架。单击按钮时,此框架的内容将发生变化。因此,如果我单击按钮显示私人客户,框架内容将显示私人客户的网站:

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

After clicking this Button the Frame will change the Content. The new Content shows Privatecustomer.privatecustomer_show.xaml now. In this xaml i have another Button. If i click this Button, the Content of the Frame in MainWindow should change to "Privatecustomer.privatecustomer_add.xaml".

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

But How I can tell from privatecustomer_show.xaml to MainWindow.xaml, that the Button addPrivatecustomer in privatecustomer_show is clicked and that the Main.Content have to change to

但是我如何从privatecustomer_show.xaml告诉MainWindow.xaml,单击privatecustomer_show中的Button addPrivatecustomer并且Main.Content必须更改为

Main.Content = new Privatecustomer.privatecustomer_add();

? I hope I can get some help here

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

2 个解决方案

#1


0  

How about adding an event in privatecustomer_show.xaml that the MainWindow.xaml can subscribe to.

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

Like this

  public event EventHandler AddPrivateCustomer;

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

Update: Please note the updated code, I made a copy'n'paste mistake in my first version.

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

Change your: private void privatecustomer_Click(object sender, RoutedEventArgs e)

更改你的:private void privatecustomer_Click(object sender,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
}

The idea is that your privatecustomer_show control sends events for when it want to change something outside of what it has access to. You MainWindow which has full control of all its child windows will then subscribe to each event.

我们的想法是,您的privatecustomer_show控件会发送事件,以便在想要更改其访问权限之外的内容时进行更改。您可以完全控制其所有子窗口的MainWindow将订阅每个事件。

#2


0  

Set Page2.Tag property to the instance of your MainWindow (use Binding in Xaml, or simply set it in code). After the specified Button in Page2 is clickd, simply use the Tag property (which is now your MainWindow).

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

Another option is to use

另一种选择是使用

App.Current.MainWindow

When the Button is clicked. (A warning. I don't know the structure your project and this might not be the instance you are looking for).

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

#1


0  

How about adding an event in privatecustomer_show.xaml that the MainWindow.xaml can subscribe to.

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

Like this

  public event EventHandler AddPrivateCustomer;

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

Update: Please note the updated code, I made a copy'n'paste mistake in my first version.

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

Change your: private void privatecustomer_Click(object sender, RoutedEventArgs e)

更改你的:private void privatecustomer_Click(object sender,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
}

The idea is that your privatecustomer_show control sends events for when it want to change something outside of what it has access to. You MainWindow which has full control of all its child windows will then subscribe to each event.

我们的想法是,您的privatecustomer_show控件会发送事件,以便在想要更改其访问权限之外的内容时进行更改。您可以完全控制其所有子窗口的MainWindow将订阅每个事件。

#2


0  

Set Page2.Tag property to the instance of your MainWindow (use Binding in Xaml, or simply set it in code). After the specified Button in Page2 is clickd, simply use the Tag property (which is now your MainWindow).

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

Another option is to use

另一种选择是使用

App.Current.MainWindow

When the Button is clicked. (A warning. I don't know the structure your project and this might not be the instance you are looking for).

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