== 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 application's ''MainWindow.xaml'': Attach menu event programmatically (in C#): 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 { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); // Attach menu event handler popupmnu1.MenuItemSelected += new RoutedEventHandler(ucPopupMnu_MenuItemSelected); } /// /// Event handler for Menu Item Selection in Popup menu. /// /// /// 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(); } } } If the event is defined with XAML (''MainWindows.xaml''), then: The code-behind looks like (''MainWindows.xaml.cs''): 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 { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); // This next line should not be present, since XAML will add it automatically in InitializeComponent() //popupmnu1.MenuItemSelected += new RoutedEventHandler(ucPopupMnu_MenuItemSelected); } /// /// Event handler for Menu Item Selection in Popup menu. /// /// /// 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(); } } } It contains 2 user controls: * ''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''. 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'' ''ucContent.xaml'' ucContent.xaml.cs: 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 { /// /// Interaction logic for ucContent.xaml /// public partial class ucContent : UserControl { //----------------------------------------------------------------------------------------- /// /// Get or set the Selected Tab Item by providing the tab label (string). /// //----------------------------------------------------------------------------------------- public string SelectedTabItem { get { TabItem tab = (TabItem)tcMain.SelectedItem; return tab.Header.ToString(); } set { switch(value) { case "Red": tcMain.SelectedItem = tabRed; break; case "Green": tcMain.SelectedItem = tabGreen; break; case "Blue": tcMain.SelectedItem = tabBlue; break; } } } //----------------------------------------------------------------------------------------- /// /// Constructor /// //----------------------------------------------------------------------------------------- public ucContent() { InitializeComponent(); } } } ''ucPopupMnu.xaml'': Select one (double click): Red Green Blue ''ucPopupMnu.xaml.cs'': 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 { /// /// Interaction logic for ucPopupMnu.xaml /// public partial class ucPopupMnu : UserControl { // Register the routed event public static readonly RoutedEvent MenuItemSelectedEvent = EventManager.RegisterRoutedEvent("MenuItemSelected", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ucPopupMnu)); // .NET wrapper for routed event public event RoutedEventHandler MenuItemSelected { add { AddHandler(MenuItemSelectedEvent, value); } remove { RemoveHandler(MenuItemSelectedEvent, value); } } public ListBoxItem SelectedItem { get { return (ListBoxItem)lstColors.SelectedItem; } set { lstColors.SelectedItem = value; } } public ucPopupMnu() { InitializeComponent(); } private void lstColors_MouseDoubleClick(object sender, MouseButtonEventArgs e) { ListBoxItem selection = (ListBoxItem)lstColors.SelectedItem; //txtSelectedColor.Text = selection.Content.ToString(); // Raise the routed event "selected" RaiseEvent(new RoutedEventArgs(ucPopupMnu.MenuItemSelectedEvent)); } } } == 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]]