如何从主窗口访问usercontrol [英] How to access usercontrol from mainwindow

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

问题描述

大家好,



我想问你,如何从MainWindow访问我的用户控件?



访问MainWindow我正在使用:



Hi all,

I would like to ask you, how can I access my usercontrol from MainWindow?

For accessing to MainWindow I'm using :

Dim mw As MainWindow = DirectCast(Application.Current.MainWindow, MainWindow)





我需要这个,因为我想从我的UserControl中选择特定的TabItem。 (tabitem.IsSelected = true)



提前谢谢



I need this because I would like to select particular TabItem from my UserControl. (tabitem.IsSelected= true)

Thank you in advance

推荐答案

你做错了什么。您不需要以这种方式访问​​主窗口。相反,您应该更好地创建自己的应用程序类,派生自 Application 并保留对其中主窗口的引用。此外,您必须将其保留为 MainWindow 类型的成员,而不是 Window ,以避免在代码中强制转换使用窗口。



同样的事情是关于你的用户控件。显然,您可以在其他 UIElement Window 或其他任何内容)中添加/插入控件实例。因此,该元素已经可以访问它。特别是,如果在XAML中执行此操作,请将 x:Name 属性添加到控件的实例中。这将生成VB.NET代码,其中包含您定义的名称下的窗口成员。使用此会员。



-SA
You are doing everything wrong. You don't need to access main window this way. Instead, you should better create your own application class, derived from Application and keep a reference to the main window in it. Moreover, you have to keep it as the member of the type MainWindow, not Window, to avoid casting in the code using the window.

Same thing is about your user control. Apparently, you add/insert the instance of your control in some other UIElement (Window or anything else). Hence, that element already has access to it. In particular, if you do it in XAML, add the x:Name attribute to the instance of your control. This will generate VB.NET code with a window member under the name you define. Use this member.

—SA


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

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