获取父用户控件的名称 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:12:31  来源:igfitidea点击:

Get the name of the parent user control WPF C#

c#wpfuser-controls

提问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 的信息。

enter image description here

在此处输入图片说明

采纳答案by Rwiti

Try using VisualTreeHelper.GetParentor use the recursive function here

尝试在此处使用VisualTreeHelper.GetParent或使用递归函数

回答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);

    ...
}