Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
swdev:dotnet:wpf:user_controls_and_intercommunication [2010/10/06 12:21]
smayr
swdev:dotnet:wpf:user_controls_and_intercommunication [2011/05/25 09:48] (current)
smayr
Line 2: Line 2:
  
 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'': 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>
  
-''MainWindows.xaml.cs'':+The code-behind looks like (''MainWindows.xaml.cs''):
 <code csharp> <code csharp>
 using System; using System;
Line 41: 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 254: Line 319:
 == Resources == == Resources ==
   * [[http://www.tanguay.info/web/index.php?pg=howtos&id=4|How to create WPF user controls with parameters]]   * [[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]]