Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
case "Maintenance":
                   switch (DrillDown.Header.ToString())
                   {
                       case "Security":
                           frmSecurity userControl = new frmSecurity();
                           currentContent.Content = userControl;
                           currentContent.Visibility = Visibility.Visible;
                           break;
                       case "Imports/Exports":
                           frmImports userControl2 = new frmImports();
                           currentContent.Content = userControl2;
                           currentContent.Visibility = Visibility.Visible;
                           break;
                       case "Order":
                           frmOrder userControl3 = new frmOrder();
                           currentContent.Content = userControl3;
                           currentContent.Visibility = Visibility.Visible;
                           break;
                   }
                   break;


Above is some code from my project, Maintenance is the menu header and the options below it are ones the user can select to open up different user controls that will appear on my main form. The user selects an option and it calls a usercontrol as the new content of my embedded user control of the calling window.

The problem is, my Imports / Exports UserControl, actually needs to call another userControl from within it. How do I reference currentContent (the userControl that I am using to host everything on my main form) from UserControl2's C# code, so that I can change the content. I know this is a scope issue, but can't seem to come up with a solution. If I am unclear with the description of my problem, please ask questions. Thanks in advance!
Posted

1 solution

Are you using MVVM?

There are a couple of things you could do using MVVM or not.

1) Use an event aggregator (Prism), Mediator (Cinch) or messenger (mvvm light I think). or use a singleton to provide a means of communication between two or more user controls. Basically these all relate to a publish and subscribe pattern. This is really the best way to go about controls communicating with each other.

2) Failing that, create a method, event or function in App.xaml. because this is a global class and accessible to most of your application, you can / could use that. Personally I would not recommend this but it is a solution to a problem in the short term.

some links:

MVVM Mediator Pattern[^]
http://joshsmithonwpf.wordpress.com/2009/04/06/a-mediator-prototype-for-wpf-apps/[^]
Prism EventAggregator Sample[^]
 
Share this answer
 
Comments
Gerenatian 4-Feb-13 16:12pm    
I am going to be doing some researching into the links you posted, as I have no prior experience with this kind of communication. Thank you for your help!
Sergey Alexandrovich Kryukov 4-Feb-13 16:55pm    
It would be such ab absurd: using MVVM and still writing hard-coded immediate constants in switch block. This OP's code is not programming at all; totally hopeless.
—SA
Gerenatian 4-Feb-13 17:30pm    
@Sergey I don't appreciate the down vote for my poor coding, I do hope to improve. I was not even are of MVVM style until db7uk was good enough to help me. That being said, I managed to solve my problem. Thank you for your help.

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