[英]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 ? 我在MainWindow中有一个页面转换(一个控件),我有很多用户控制页面,我想从我的用户控制页面访问MainWindow中的页面转换? 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);
// }
Application.Current.MainWindow
使用此功能,您可以从任何地方访问MainWindow。
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(...);
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.