从用户控件访问父窗口

时间:2022-10-19 22:23:22

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也就是主窗口中的mainGrid ?

5 个解决方案

#1


41  

Have you tried

你有试过

Window yourParentWindow = Window.GetWindow(userControl1);

#2


12  

This gets the root level window:

这就得到了根级别窗口:

Window parentWindow = Application.Current.MainWindow

or the immediate parent window

或直接父窗口

Window parentWindow = Window.GetWindow(this);

#3


2  

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.

我建议通过控制将作为构造函数参数运行的网格,使其成为一个属性,或者在任何窗口中动态地搜索适当的(根?)网格。

#4


1  

Thanks for help me guys. i got another solution

谢谢你们的帮助。我有另一个解决方案

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

this is perfectly works

这是完美的作品

#5


0  

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)
        }
    }

#1


41  

Have you tried

你有试过

Window yourParentWindow = Window.GetWindow(userControl1);

#2


12  

This gets the root level window:

这就得到了根级别窗口:

Window parentWindow = Application.Current.MainWindow

or the immediate parent window

或直接父窗口

Window parentWindow = Window.GetWindow(this);

#3


2  

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.

我建议通过控制将作为构造函数参数运行的网格,使其成为一个属性,或者在任何窗口中动态地搜索适当的(根?)网格。

#4


1  

Thanks for help me guys. i got another solution

谢谢你们的帮助。我有另一个解决方案

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

this is perfectly works

这是完美的作品

#5


0  

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)
        }
    }