WPF用户控件父 [英] WPF User Control Parent
本文介绍了WPF用户控件父的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有我加载到主窗口在运行时,用户的控制。我不能让包含窗口从用户控件上的手柄。
我已经试过this.Parent,但它总是空。有谁知道如何从一个WPF用户控件得到一个处理包含窗口?
下面是控制如何加载:
私人无效XMLLogViewer_MenuItem_Click(对象发件人,RoutedEventArgs E)
{
菜单项应用=发件人的菜单项;
字符串参数= application.CommandParameter为字符串;
字符串控件名称=参数;
如果(uxPanel.Children.Count == 0)
{
System.Runtime.Remoting.ObjectHandle例如= Activator.CreateInstance(Assembly.GetExecutingAssembly()全名,控件名称。);
用户控件控制= instance.Unwrap()作为用户控件;
this.LoadControl(控制);
}
}
私人无效LoadControl(用户控件的控件)
{
如果(uxPanel.Children.Count大于0)
{
的foreach(在uxPanel.Children的UIElement CTRL)
{
如果(ctrl.GetType()!= control.GetType())
{
this.SetControl(控制);
}
}
}
其他
{
this.SetControl(控制);
}
}
私人无效SetControl(用户控件的控件)
{
control.Width = uxPanel.Width;
control.Height = uxPanel.Height;
uxPanel.Children.Add(控制);
}
解决方案
尝试使用以下
窗口parentWindow = Window.GetWindow(userControlRefernce);
该GetWindow方法会走的VisualTree为你找到承载您的控件的窗口。
I have a user control that I load into a main window at runtime. I cannot get a handle on the containing window from the user control.
I have tried this.Parent, but it's always null. Does anyone know how to get a handle to the containing window from a user control in WPF?
Here is how the control is loaded:
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);
}
}
private void SetControl(UserControl control)
{
control.Width = uxPanel.Width;
control.Height = uxPanel.Height;
uxPanel.Children.Add(control);
}
解决方案
Try using the following
Window parentWindow = Window.GetWindow(userControlRefernce);
The GetWindow method will walk the VisualTree for you and locate the window that is hosting your control.
这篇关于WPF用户控件父的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文