简体   繁体   English

从用户控件访问父窗口

[英]Access parent window from User Control

I am trying to access parent window from user control. 我试图从用户控件访问父窗口。

userControl1 uc1 = new userControl1();

mainGrid.Children.Add(uc1);

through this code I load userControl1 to main grid. 通过这段代码我将userControl1加载到主网格。

But when I click on a button inside userControl1 then I want to load another userControl2 into mainGrid which is in main window? 但是当我点击userControl1一个按钮然后我想将另一个userControl2加载到主窗口中的mainGrid中?

你有没有尝试过

Window yourParentWindow = Window.GetWindow(userControl1);

This gets the root level window: 这将获得根级别窗口:

Window parentWindow = Application.Current.MainWindow

or the immediate parent window 或直接的父窗口

Window parentWindow = Window.GetWindow(this);

The only reason why the suggested 建议的唯一原因

Window yourParentWindow = Window.GetWindow(userControl1);

didnt work for you is because you didn't cast it to the right type: 没有为你工作是因为你没有把它强制转换为正确的类型:

var win = Window.GetWindow(this) as MyCustomWindowType;

if (win != null) {
    win.DoMyCustomWhatEver()
} else {
    ReportError("Tough luck, this control works only in descendants of MyCustomWindowType");
}

Unless there has to be way more coupling between your type of windows and your control, I consider your approach bad design . 除非你的窗户和你的控制之间必须有更多的耦合,否则我认为你的方法设计不好。

I'd suggest to pass the grid on which the control will operate as a constructor parameter, make it into a property or search for appropriate (root ?) grid inside any Window dynamically. 我建议传递控件将作为构造函数参数运行的网格,将其作为属性或动态搜索任何Window相应(根?)网格。

Modify the constructor of the UserControl to accept a parameter of MainWindow object. 修改UserControl的构造函数以接受MainWindow对象的参数。 Then pass the MainWindow object to the UserControl when creating in the MainWindow. 然后在MainWindow中创建时将MainWindow对象传递给UserControl。

MainWindow 主窗口

public MainWindow(){
    InitializeComponent();
    userControl1 uc1 = new userControl1(this);
}

UserControl 用户控件

MainWindow mw;
public userControl1(MainWindow recievedWindow){
    mw = recievedWindow;
}

Example Event in the UserControl UserControl中的示例事件

private void Button_Click(object sender, RoutedEventArgs e)
{
    mw.mainGrid.Children.Add(this);
}

Thanks for help me guys. 谢谢你们的帮助。 i got another solution 我有另一种解决方案

 ((this.Parent) as Window).Content = new userControl2();

this is perfectly works 这是完美的作品

Make a static instance of main window ,you can simply call it in your user control: 创建主窗口的静态实例,您只需在用户控件中调用它:

See this example: 看这个例子:

Window1.cs Window1.cs

 public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            _Window1 = this;
        }
        public static Window1 _Window1 = new Window1();

    }

UserControl1.CS UserControl1.CS

public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();

        }
        private void AddControl()
        {
           Window1._Window1.MainGrid.Children.Add(usercontrol2)
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM