Update to try and help Greg in the comments. The command works in the Windows main menu, in the user control and in the context menu of the user control.
I would do it with commands. So the Commands.cs class has something like:
public static class Commands { public static RoutedUICommand TestShowDialogCommand = new RoutedUICommand("Test command", "TestShowDialog", typeof(Commands)); }
Register them in your main window: (you do not need the default canshow line to be true)
public Window1() { InitializeComponent(); CommandManager.RegisterClassCommandBinding(typeof(System.Windows.Controls.Control), new CommandBinding(Commands.TestShowDialogCommand, ShowDialogCommand, CanShowDialogCommand)); } private void ShowDialogCommand(object sender, ExecutedRoutedEventArgs e) { var box = new Window(); box.Owner = this; box.ShowDialog(); } private void CanShowDialogCommand(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = true; }
This is my xaml for the main window:
<Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:WpfApplication1="clr-namespace:WpfApplication1" Title="Window1" Height="300" Width="322"> <Grid> <StackPanel> <Menu> <MenuItem Header="Test"> <MenuItem Header="ShowDialog" Command="{x:Static WpfApplication1:Commands.TestShowDialogCommand}"/> </MenuItem> </Menu> <WpfApplication1:BazUserControl /> </StackPanel> </Grid> </Window>
This is xaml for my user control (default only)
<UserControl x:Class="WpfApplication1.BazUserControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:WpfApplication1="clr-namespace:WpfApplication1" Height="300" Width="300"> <Grid> <StackPanel> <Button Command="{x:Static WpfApplication1:Commands.TestShowDialogCommand}" Content="ClickMe" ></Button> <TextBox> <TextBox.ContextMenu> <ContextMenu> <MenuItem Header="ShowDialog" Command="{x:Static WpfApplication1:Commands.TestShowDialogCommand}" /> </ContextMenu> </TextBox.ContextMenu> </TextBox> </StackPanel> </Grid> </UserControl>
You can do this a little further and process the command in the controller class instead and make the bit bigger than MVC.
Andrew Barrett Mar 03 '09 at 17:51 2009-03-03 17:51
source share