C# 从用户控制访问父窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16236905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:29:10  来源:igfitidea点击:

Access parent window from User Control

c#.netwpf

提问by Lazy Lion

I am trying to access parent window from user control.

我正在尝试从用户控件访问父窗口。

userControl1 uc1 = new userControl1();

mainGrid.Children.Add(uc1);

through this code I load userControl1to main grid.

通过这段代码我加载userControl1到主网格。

But when I click on a button inside userControl1then I want to load another userControl2into mainGridwhich is in main window?

但是,当我点击一个按钮里面userControl1,然后我想在另一userControl2mainGrid这是在主窗口?

回答by Vlad Bezden

Have you tried

你有没有尝试过

Window yourParentWindow = Window.GetWindow(userControl1);

回答by KF2

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

用户控件1.CS

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

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

回答by Lazy Lion

Thanks for help me guys. i got another solution

谢谢各位帮我。我有另一个解决方案

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

this is perfectly works

这是完美的作品

回答by seabass2020

This gets the root level window:

这将获取根级别窗口:

Window parentWindow = Application.Current.MainWindow

or the immediate parent window

或直接父窗口

Window parentWindow = Window.GetWindow(this);

回答by Miloslav Raus

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 beway 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 Windowdynamically.

我建议传递控件将在其上作为构造函数参数运行的网格,使其成为一个属性或在任何Window动态内搜索适当的(根?)网格。

回答by Suneth Thotagamuwa

Modify the constructor of the UserControl to accept a parameter of MainWindow object. Then pass the MainWindow object to the UserControl when creating in the MainWindow.

修改 UserControl 的构造函数以接受 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);
}