从用户控制页面访问 MainWindow 的控件。WPF C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17562358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accessing MainWindow's controls from a user control page. WPF C#
提问by user2376998
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);
// }
回答by Jawahar
Application.Current.MainWindow
Using this you can access the MainWindow from any place.
使用它,您可以从任何地方访问 MainWindow。
回答by Romano Zumbé
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(...);
回答by Muhamed Shafeeq
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();
}

