获取父用户控件的名称 WPF C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17623399/
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
Get the name of the parent user control WPF C#
提问by Anthony Russell
I have access to User Control A. I want to get info on User Control C. Is there any way to do this in WPF? The structure is basically what you see. User Control D is a ribbon, C is a tab on the ribbon and B and A are contents of C. I can't seem to get access to C. I tried using the Parent property of A but it doesn't seem to give me the info on C.
我有权访问用户控件 A。我想获取有关用户控件 C 的信息。在 WPF 中有没有办法做到这一点?结构基本上就是你所看到的。用户控件 D 是功能区,C 是功能区上的选项卡,B 和 A 是 C 的内容。我似乎无法访问 C。我尝试使用 A 的 Parent 属性,但似乎没有我 C 的信息。


采纳答案by Rwiti
回答by Guilherme H. J.
Maybe you can try to cast the parent as UserControl C, like this:
也许您可以尝试将父级转换为 UserControl C,如下所示:
(this.Parent as UserControlC).YourProperty
回答by Mehdi Benkirane
use the Window.GetWindow(this) method within Loaded event handler.
在 Loaded 事件处理程序中使用 Window.GetWindow(this) 方法。
public MainView()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainView_Loaded);
}
void MainView_Loaded(object sender, RoutedEventArgs e)
{
Window parentWindow = Window.GetWindow(this);
...
}

