Access to MainWindow items from a custom control panel. WPF C #

I have a page transition (control) in MainWindow, I have many user control pages, do I want to access a page transition in MainWindow from my user control page? How can I do it? I tried:

Story page = new Story(); NavigationService nav = NavigationService.GetNavigationService(this); // Navigate to the page, using the NavigationService // if (nav != null) // { // nav.Navigate(page); MainWindow test = new MainWindow(); test.pageTransition1.ShowPage(page); // } 
+4
source share
3 answers
 Application.Current.MainWindow 

Using this, you can access MainWindow from anywhere.

+10
source

You can find the WpfPageTransitions.PageTransition control, like this from the UserControls code:

 public static WpfPageTransitions.PageTransition FindPageControl(DependencyObject child) { DependencyObject parent= VisualTreeHelper.GetParent(child); if (parent == null) return null; WpfPageTransitions.PageTransition page = parent as WpfPageTransitions.PageTransition; if (page != null) { return page; } else { return FindPageControl(parent); } } 

Then you can use it as follows:

 this.FindPageControl(this).ShowPage(...); 
+2
source

Create a method in the main window to select a page transition

  public void ChangePage() { pageTransitionControl.ShowPage(new NewData()); } 

Then in Child control

 private void btnUpdate_Click(object sender,RoutedEventArgs e) { MainWindow win = (MainWindow)Window.GetWindow(this); win.ChangePage(); } 
0
source

Source: https://habr.com/ru/post/1490622/


All Articles