相关疑难解决方法(0)

WPF用户控制父级

我有一个用户控件,我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)

.net c# wpf

180
推荐指数
9
解决办法
15万
查看次数

标签 统计

.net ×1

c# ×1

wpf ×1