从用户控制页面访问MainWindow的控件。 WPF C#

时间:2022-12-25 15:53:19

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 :

我在MainWindow中有一个页面转换(一个控件),我有很多用户控制页面,我想从我的用户控制页面访问MainWindow中的页面转换?我怎么做?我试过了 :

        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);

    //    }

3 个解决方案

#1


10  

Application.Current.MainWindow

Using this you can access the MainWindow from any place.

使用此功能,您可以从任何地方访问MainWindow。

#2


2  

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

您可以从后面的UserControls代码中找到这样的WpfPageTransitions.PageTransition控件:

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(...);

#3


0  

Create A Method Inside Main Window for Choosing 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();
        }

#1


10  

Application.Current.MainWindow

Using this you can access the MainWindow from any place.

使用此功能,您可以从任何地方访问MainWindow。

#2


2  

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

您可以从后面的UserControls代码中找到这样的WpfPageTransitions.PageTransition控件:

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(...);

#3


0  

Create A Method Inside Main Window for Choosing 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();
        }