WPF 获取 UserControl 所有者(容器元素)

标签 wpf user-controls

所以我有一个位于另一个用户控件内部的用户控件。我们称它们为 ParentUC 和 ChildUC。我需要从 ChildUC 获取 ParentUC。

我知道获取窗口所有者的方法是Window.GetWindow(userControl),但是UserControl没有这样的方法,据我所知。

感谢您的帮助!

最佳答案

我想出了这个解决方案,但如果您有更好的解决方案,请发布。谢谢!

DependencyObject ucParent = this.Parent;

while (!(ucParent is UserControl))
{
    ucParent = LogicalTreeHelper.GetParent(ucParent);
}

关于WPF 获取 UserControl 所有者(容器元素),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1474438/

相关文章:

c# - WPF - 使用 pack URI 从转换器返回图像

wpf - style 根据背景颜色选择合适的前景

c# - Entity Framework 6/SQL Server CE 4 SaveChangesAsync()

c# - 通过 MVVM 模式创建 UserControl - DataContext 并绑定(bind)到父级

c# - 从父页面验证 ASP.NET 用户控件

c# - 如何在 wpf 中显示用户对按钮单击的控制?

c# - 包含 ComboBox 和 Items setter 的 wpf 控件

WPF <StatusBar> 未位于窗口底部

asp.net - 使用用户控件中的函数(razor 语法)

c# - 'MyUserControls.ComboBox 1' does not contain definition for ' ItemsSource'