Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 如何在silverlight中访问usercontrol中的父页面控件?_Wpf_Silverlight_Windows 8 - Fatal编程技术网

Wpf 如何在silverlight中访问usercontrol中的父页面控件?

Wpf 如何在silverlight中访问usercontrol中的父页面控件?,wpf,silverlight,windows-8,Wpf,Silverlight,Windows 8,我想访问用户控件(子页)中的主页网格值。请告诉我如何执行此操作?您可以在App类(在App.xaml.cs中声明)上公开您的mainpage,作为App.mainpage属性: public MainPage MainPage { get; private set; } private void Application_Startup(object sender, StartupEventArgs e) { this.MainPage = new MainPage(); thi

我想访问用户控件(子页)中的主页网格值。请告诉我如何执行此操作?

您可以在
App
类(在
App.xaml.cs
中声明)上公开您的
mainpage
,作为
App.mainpage
属性:

public MainPage MainPage { get; private set; }

private void Application_Startup(object sender, StartupEventArgs e)
{
    this.MainPage = new MainPage();
    this.RootVisual = this.MainPage;
}
然后,您可以访问MainPage类上所有公开的属性/函数,如下所示:

MainPage mainPage = ((App)App.Current).MainPage;
using System.Linq;
using System.Windows.Controls.Primitives;


// place this code where you want to find MainPage
MainPage mainPage = this.GetVisualAncestors().OfType<MainPage>().FirstOrDefault();
或者,您可以在页面的可视树中找到主页,如下所示:

MainPage mainPage = ((App)App.Current).MainPage;
using System.Linq;
using System.Windows.Controls.Primitives;


// place this code where you want to find MainPage
MainPage mainPage = this.GetVisualAncestors().OfType<MainPage>().FirstOrDefault();
使用System.Linq;
使用System.Windows.Controls.Primitives;
//将此代码放置在要查找主页的位置
MainPage MainPage=this.getVisualanceTors().OfType().FirstOrDefault();

您是否使用模型-视图-视图-模型模式,或者您的数据是否存储在页面/控制代码中?我没有使用MVVM模型。仅使用普通页面代码,而这些代码在示例中不起作用,第一个或第二个?在windows 8中没有应用程序启动事件。我无法在windows 8 app.xaml.cs页面中写入此内容。你知道windows 8应用程序吗?你是否在windows 8下开发windows 8 WPF或Silverlight应用程序?因为你的题目是“在Silverlight中”。。。无论如何,您也可以尝试使用visual tree的第二个选项。如果您关心性能,请在“类”字段中存储下一次使用的值。