Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing MainWindow's controls from a user control page. WPF C#

I have a page transition ( a control ) in the MainWindow , I have many user control pages , I want to access the page transition in the MainWindow from my user control page ? How do I do that? 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);

    //    }
like image 205
user2376998 Avatar asked Dec 09 '22 14:12

user2376998


2 Answers

Application.Current.MainWindow

Using this you can access the MainWindow from any place.

like image 96
Jawahar Avatar answered Dec 11 '22 08:12

Jawahar


You could find the WpfPageTransitions.PageTransition control like this from the UserControls code behind:

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 like this:

this.FindPageControl(this).ShowPage(...);
like image 40
Romano Zumbé Avatar answered Dec 11 '22 07:12

Romano Zumbé