Differences
This shows you the differences between two versions of the page.
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 | + | For applications that are composed of multiple user controls, there needs to be coordination and communication between them. For example, an application' |
+ | <code xml> | ||
+ | <Window x: | ||
+ | xmlns=" | ||
+ | xmlns: | ||
+ | xmlns: | ||
+ | Title=" | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | 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 | ||
+ | { | ||
+ | /// < | ||
+ | /// 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. | ||
+ | /// </ | ||
+ | /// <param name=" | ||
+ | /// <param name=" | ||
+ | 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 ('' | ||
<code xml> | <code xml> | ||
<Window x: | <Window x: | ||
Line 15: | Line 77: | ||
</ | </ | ||
+ | The code-behind looks like ('' | ||
<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() | ||
+ | // | ||
} | } | ||
Line 60: | Line 126: | ||
It contains 2 user controls: | 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. | + | The '' |
- | This is how it looks: ucContent.xaml | + | This is how it looks: |
+ | |||
+ | '' | ||
<code xml> | <code xml> | ||
Line 157: | Line 225: | ||
</ | </ | ||
- | ucPopupMnu.xaml: | + | '' |
<code xml> | <code xml> | ||
Line 190: | Line 258: | ||
</ | </ | ||
- | ucPopupMnu.xaml.cs: | + | '' |
<code csharp> | <code csharp> | ||
Line 248: | Line 316: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | == Resources == | ||
+ | * [[http:// | ||
+ | * [[http:// |