开发者

How the Page call the function in a Window

开发者 https://www.devze.com 2023-01-12 19:37 出处:网络
I\'m writting a WPF program.And now i have some problems. There is a Window including a frame, with two Buttons named A and B.

I'm writting a WPF program.And now i have some problems. There is a Window including a frame, with two Buttons named A and B.

Within the frame there is a Page that has a Button C.

The question is: how can I have the A button's event handler fire when i click the C Button in the Page?

Frame xaml:

<Frame x:Name="Frame1" 
  NavigationUIVisibility="Hidden" 
  Margin="-50,0,-163,-11" 
  Grid.RowSpan="2">
</Frame>

Event handlers:

 private void button1_Click(object sender, RoutedEventArgs e)
 {
     PageFind p = new Pa开发者_如何学CgeFind();
     this.Frame1.Content = p;
 }
 private void button2_Click(object sender, RoutedEventArgs e)
 {
   MessageBox.Show("Succeed!!")
 } 


There are lots of ways of doing this. I won't go into what I believe would be the best way (see Model-View-ViewModel) and try to answer your question.

The Window and the Page instances must be able to "find" each other, so that the Window can pass the Page a method to execute when the C button is clicked, or so that the Window can register an event with the Page that will be fired when the C button is clicked.

The question is, does the Window ever access the Page instance directly, or is your application using the Navigation framework of the Frame to handle loading your Pages?

If the first, it would be trivial for the Window to register with an event you can place on the public face of your Page class. If the second, you'll have to listen to the navigation service of the Frame in order to find the Page instance.

Another alternative is that the DataContext of the Window flows to the Page. You can create a class that both the Window and the Page can bind against, so that when the Page's button is clicked it can call a method in that context class which could then notify the Window that the button was clicked.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号