我有一个用户控件,我MainWindow在运行时加载.我无法从包含窗口获取句柄UserControl.
我试过了this.Parent,但它总是空的.有谁知道如何从WPF中的用户控件获取包含窗口的句柄?
以下是控件的加载方式:
private void XMLLogViewer_MenuItem_Click(object sender, RoutedEventArgs e)
{
MenuItem application = sender as MenuItem;
string parameter = application.CommandParameter as string;
string controlName = parameter;
if (uxPanel.Children.Count == 0)
{
System.Runtime.Remoting.ObjectHandle instance = Activator.CreateInstance(Assembly.GetExecutingAssembly().FullName, controlName);
UserControl control = instance.Unwrap() as UserControl;
this.LoadControl(control);
}
}
private void LoadControl(UserControl control)
{
if (uxPanel.Children.Count > 0)
{
foreach (UIElement ctrl in uxPanel.Children)
{
if (ctrl.GetType() != control.GetType())
{
this.SetControl(control);
}
}
}
else
{
this.SetControl(control);
} …Run Code Online (Sandbox Code Playgroud) MainPage.xaml
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid x:Name="GridA">
<Grid x:Name="GridB"/>
</Grid>
</Grid>
Run Code Online (Sandbox Code Playgroud)
有可能从中得到GridB父母GridB
这就是我想要做的
//Null
Panel parent1 = GridB.Parent as Panel;
//Null
Panel parent2 = VisualTreeHelper.GetParent(GridB) as Panel;
Run Code Online (Sandbox Code Playgroud)
所有这些都返回null.
任何的想法?