Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
swdev:dotnet:wpf:user_controls_and_intercommunication [2010/09/30 16:31]
smayr created
swdev:dotnet:wpf:user_controls_and_intercommunication [2011/05/25 09:48] (current)
smayr
Line 1: Line 1:
 == User Controls and Intercommunication == == User Controls and Intercommunication ==
  
-For applications that are composed of multiple user controls, there needs to be coordination and communication between them.  For example, an applications MainWindow:+For applications that are composed of multiple user controls, there needs to be coordination and communication between them.  For example, an application's ''MainWindow.xaml'': 
 +<code xml> 
 +<Window x:Class="InterUsrCtrlComm.MainWindow" 
 +        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
 +        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
 +        xmlns:local="clr-namespace:InterUsrCtrlComm" 
 +        Title="Inter User Control Communication" Height="350" Width="525"> 
 +    <Grid> 
 +        <local:ucContent x:Name="content1"/> 
 +        <local:ucPopupMnu x:Name="popupmnu1"/> 
 +    </Grid> 
 +</Window> 
 +</code> 
 + 
 +Attach menu event programmatically (in C#): 
 +<code csharp> 
 +using System; 
 +using System.Collections.Generic; 
 +using System.Linq; 
 +using System.Text; 
 +using System.Windows; 
 +using System.Windows.Controls; 
 +using System.Windows.Data; 
 +using System.Windows.Documents; 
 +using System.Windows.Input; 
 +using System.Windows.Media; 
 +using System.Windows.Media.Imaging; 
 +using System.Windows.Navigation; 
 +using System.Windows.Shapes; 
 + 
 +namespace InterUsrCtrlComm 
 +
 +    /// <summary> 
 +    /// Interaction logic for MainWindow.xaml 
 +    /// </summary> 
 +    public partial class MainWindow : Window 
 +    { 
 +        public MainWindow() 
 +        { 
 +            InitializeComponent(); 
 +         
 +            // Attach menu event handler 
 +            popupmnu1.MenuItemSelected += new RoutedEventHandler(ucPopupMnu_MenuItemSelected); 
 +        } 
 +         
 +        /// <summary> 
 +        /// Event handler for Menu Item Selection in Popup menu. 
 +        /// </summary> 
 +        /// <param name="sender"></param> 
 +        /// <param name="e"></param> 
 +        private void ucPopupMnu_MenuItemSelected(object sender, RoutedEventArgs e) 
 +        { 
 +            // Get menu selection 
 +            ListBoxItem selection = popupmnu1.SelectedItem; 
 + 
 +            // Take action based on the menu selection 
 +            content1.SelectedTabItem = selection.Content.ToString(); 
 +        } 
 +   } 
 +
 +</code> 
 + 
 +If the event is defined with XAML (''MainWindows.xaml''), then:
 <code xml> <code xml>
 <Window x:Class="InterUsrCtrlComm.MainWindow" <Window x:Class="InterUsrCtrlComm.MainWindow"
Line 15: Line 77:
 </code> </code>
  
 +The code-behind looks like (''MainWindows.xaml.cs''):
 <code csharp> <code csharp>
 using System; using System;
Line 40: Line 103:
         {         {
             InitializeComponent();             InitializeComponent();
 +            
 +            // This next line should not be present, since XAML will add it automatically in InitializeComponent()
 +            //popupmnu1.MenuItemSelected += new RoutedEventHandler(ucPopupMnu_MenuItemSelected);
         }         }
  
Line 60: Line 126:
  
 It contains 2 user controls: It contains 2 user controls:
-  * ucContent which has the main content for the application. +  * ''ucContent'' which has the main content for the application. 
-  * ucPopupMnu which has a popup menu that triggers events that should affect the content in ucContent.+  * ''ucPopupMnu'' which has a popup menu that triggers events that should affect the content in ''ucContent''.
  
-The MainWindow coordinates communication between these user controls.  ucPopupMnu has routed events that are then intercepted by MainWindow, and from here trigger some action in ucContent.+The ''MainWindow'' coordinates communication between these user controls.  ucPopupMnu has routed events that are then intercepted by ''MainWindow'', and from here trigger some action in ''ucContent''.
  
-This is how it looks: ucContent.xaml+This is how it looks: ''ucContent'' 
 + 
 +''ucContent.xaml''
 <code xml> <code xml>
  
Line 157: Line 225:
 </code> </code>
  
-ucPopupMnu.xaml:+''ucPopupMnu.xaml'':
 <code xml> <code xml>
  
Line 190: Line 258:
 </code> </code>
  
-ucPopupMnu.xaml.cs:+''ucPopupMnu.xaml.cs'':
 <code csharp> <code csharp>
  
Line 248: Line 316:
     }     }
 </code> </code>
 +
 +== Resources ==
 +  * [[http://www.tanguay.info/web/index.php?pg=howtos&id=4|How to create WPF user controls with parameters]]
 +  * [[http://msdn.microsoft.com/en-us/library/ms742806.aspx|MSDN: Router Events Overview]]