從用戶控制頁面訪問MainWindow的控件。 WPF C#

[英]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 :

我在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();
        }

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/07/10/72f0cfae02682b799e2efc6346bdfb22.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com