如何从Usercontrol访问父级 [英] How Do I Access Parent From Usercontrol

查看:59
本文介绍了如何从Usercontrol访问父级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简介:

好​​的,所以我现在正在使用C#WPF,我得到的是一个mainWindow和一些不同的用户控件,mainWindow有一个网格,它是孩子们正在添加和删除UserControl。



但是我希望能够从默认(或首次加载)中删除并向此mainWindow网格添加不同的UserControl用户控件。例如,从UserControl内部的按钮单击,一旦点击更改为特定的不同UserControl并加载到mainWindow Grid.Children属性。



我试过:

Window parent = this.Parent;

Window parentWindow = Window.GetWindow(userControlReference) ;



Intro:
Ok, so i'm currently working in C# WPF, what i've got is a mainWindow and a number of different usercontrols, The mainWindow has got a grid of which it's children are having UserControls added and removed.

However i am wanting to be able to remove and add a different UserControl to this mainWindow grid from within the default (or first loaded) UserControl. e.g from a button click inside the UserControl, once clicked changes to a specific different UserControl and loads that in the mainWindow Grid.Children attribute.

I've tried:
Window parent = this.Parent;
Window parentWindow = Window.GetWindow(userControlReference);

public UserControl()<br />
{<br />
     InitializeComponent();<br />
     this.Loaded += new RoutedEventHandler(UserControl_Loaded);<br />
}<br />
<br />
public void UserControl_Loaded(object sender, RoutedEventArgs e)<br />
{<br />
      Window parentWindow = Window.GetWindow(this);<br />
      MessageBox.Show(parentWindow.ToString());<br />
}





上面的i试图看看它返回的是什么,它返回了我的mainWindow(ProjectTest.Loader.Views.MainWindow)的位置引用,但因为它只是locatio参考(至少我认为这就是所谓的,如果我错了就纠正我)我无法使用实际窗口本身的任何控件或属性。



那么我有什么方法可以从UserControl子(Grid Child)访问Grid和/或mainWindow



感谢您的帮助:)



The above i tried to see what it was returning and it returned the location reference of my mainWindow (ProjectTest.Loader.Views.MainWindow) however as it is only the location reference (atleast i think that's what it's called, correct me if i'm wrong) I'm unable to use any controls or properties from the actual window itself.

So what ways are there for me to access the Grid and or mainWindow from the UserControl child (Grid Child)

Thanks for any help :)

推荐答案

你试过这个吗?

如何在wpf中获得父控件? [ ^ ]



似乎是同一个问题,只是做一些与信息略有不同的事情。



另外检查一下:

http://www.haidermrizvi.com/getting-parent-control-of-a-control-in-wpf-through-code-or-at-runtime/# .VWVMK09VhBc [ ^ ]
Have you tried this?
How to get parent control in wpf?[^]

seems to be the same issue, just doing something slightly different with the information.

Also check this out:
http://www.haidermrizvi.com/getting-parent-control-of-a-control-in-wpf-through-code-or-at-runtime/#.VWVMK09VhBc[^]


在我的感觉中,你想做的事情是错误的。

UserControl通常是一个元素,由Parent-Cl使用屁股(例如一个窗口) - 不是使用它的父元素。



如果你想访问父母的元素,你必须知道它们,你有通过Parent.Controls-Collection创建对每个参考的引用。

如果这样做,你的UserControl不再具有通用性 - 它直接依赖于Parent-Class - 而且只对那个class(或以相同方式构建的类)。
In my Feeling it is the wrong way what you want to do.
A UserControl normally is an element, which is used by/from a Parent-Class (for example a window) - not an element which uses it's Parent.

If you want to Access the elements of the Parent you have to know them and you have to create a Reference to each of them via the Parent.Controls-Collection.
If you do this your UserControl is not longer universal - it has a direct dependancy to the Parent-Class - and only to that class (or a class which is build up in the same way).


这篇关于如何从Usercontrol访问父级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆