Click here to Skip to main content
15,920,513 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,

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

For accessing to MainWindow I'm using :

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


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

Thank you in advance
Posted

1 solution

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
 
Share this answer
 
Comments
Maciej Los 23-Jul-14 13:47pm    
Thank you for clarification, Sergey ;)
+5!
Sergey Alexandrovich Kryukov 23-Jul-14 16:51pm    
Thank you, Maciej.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900