What can break WPF CommandBindings on a MenuItem? - wpf

I have some CommandBindings (in a Window) that work with MenuItems (by "work" I mean the executed/canexecute handlers are called),
I have others (in a UserControl) that work when assigned to the Command properties of Buttons (the handlers are called) -- but not when used with MenuItems (the handlers are never called).
I can make a MenuItem interact correctly with a CommandBinding in the UserControl by copying and pasting each binding into the appropriate MenuItem.CommandBindings, like so:
<MenuItem
Header="Select All"
Command="{StaticResource SelectAllCommand}"
>
<MenuItem.CommandBindings>
<CommandBinding
Command="{StaticResource SelectAllCommand}"
Executed="SelectAll_Executed"
CanExecute="SelectAll_CanExecute"
/>
</MenuItem.CommandBindings>
</MenuItem>
But that's silly (and see below).
I can also make them work by copying the UserControl's command bindings up to the window in the UserControl's constructor:
C#
Application.Current.MainWindow.CommandBindings.AddRange(this.CommandBindings);
Again, that's pretty crazy, but it does seem to imply that there's a contextual factor at work here.
I copied the relevant bits of the control XAML into the following test XAML to reproduce the issue, but the issue didn't reproduce. Unlike the production code I extracted it from, it works as you'd expect: It's a binding, it's bound, it calls the methods. But the exact same method of binding event handler methods to commands to menuitems fails in a different UserControl in a different (and incompably vaster and more complicated) project.
XAML:
<UserControl
x:Class="CommandTest.TestControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="300"
>
<UserControl.Resources>
<ResourceDictionary>
<RoutedUICommand x:Key="TestCommand" />
<ContextMenu x:Key="TestMenu">
<MenuItem
Header="_Test"
Command="{StaticResource TestCommand}"
/>
</ContextMenu>
</ResourceDictionary>
</UserControl.Resources>
<UserControl.CommandBindings>
<CommandBinding
Command="{StaticResource TestCommand}"
Executed="TestCommand_Executed"
CanExecute="TestCommand_CanExecute"
/>
</UserControl.CommandBindings>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBox
Width="200"
ContextMenu="{StaticResource TestMenu}"
/>
</Grid>
</UserControl>
C#:
private void TestCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Test Command", "Test", MessageBoxButton.OK,
MessageBoxImage.Information);
}
private void TestCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
e.Handled = true;
}
So the question is, what hidden factors can cause a CommandBinding to fail silently? How do you even debug this? Why would I see completely different behavior between a Button and a MenuItem for the same command binding in the same control? Is it because the ContextMenu is a resource? But in my test code, it's a resource and everything works.
UPDATE:
Another solution^Wworkaround: Explicitly set the MenuItem.PlacementTarget to the ContextMenu. Huh.

I suspect there is a subtle difference between the visual trees of your test project and your production project that is interfering with the command routing. Routed Commands search for command handlers along the visual tree, and up to two paths may be taken.
From this MSDN Magazine article:
Usually, a command invoker looks for a command binding between its own location in the visual tree and the root of the visual tree. If it finds one, the bound command handler will determine whether the command is enabled and will be called when the command is invoked. If the command is hooked up to a control inside a toolbar or menu (or, more generally, a container that sets FocusManager.IsFocusScope = true), then some additional logic runs that also looks along the visual tree path from the root to the focus element for a command binding.

Related

Xaml: design a layout with dynamically visible components

In a mvvm application some areas inside a window (in reality it is a UserControl inside MainWindow) are dynamically displayed according to the user selections.
The changing blocks are inside Stackpanels, I have 4 of them and only one at a time is displayed. This is accomplished binding Visibility to a bool property and using the BooleanToVisibilityConverter.
I put all the alternate StackPanel inside parent control. It works correctly, but during design phase in Visual Studio I see all of them, so I have problems in figuring the final layout.
How can I easily create the layout having more controls which share the same window area and are displayed one at a time ?
Setting A Design Time Only Data Context
Developing XAML in the Studio Designer can be greatly simplified by setting the Design-Time Data Context.
One implementation is based on setting a duplicate DataContext which will be ignored during the final compilation.
To implement the switching, add to the ViewModel, a property that will inform the designer whether it can be used in Development Mode or not.
I use an MVVMLight situation for this example, but for this declared instance property IsInDesignMode and static property ViewModelBase.IsInDesignModeStatic.
Example:
using System.ComponentModel;
namespace DataContextDesignTime.Example
{
public class MyViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private bool _flag;
public bool Flag
{
get => _flag;
set
{
if (!Equals(_flag, value))
{
_flag = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Flag)));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(NotFlag)));
}
}
}
public bool NotFlag => !Flag;
}
}
<Window x:Class="DataContextDesignTime.Example.ExamleWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DataContextDesignTime.Example"
mc:Ignorable="d"
Title="ExamleWindow" Height="450" Width="800">
<d:Window.DataContext>
<local:MyViewModel Flag="True" NotFlag="True"/>
</d:Window.DataContext>
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
</Window.Resources>
<StackPanel>
<Border Background="LightBlue" Height="200"
Visibility="{Binding Flag, Converter={StaticResource BooleanToVisibilityConverter}}"/>
<Border Background="LightGreen" Height="400"
Visibility="{Binding NotFlag, Converter={StaticResource BooleanToVisibilityConverter}}"/>
</StackPanel>
</Window>
In this example, you can change property values in XAML or in the Property Browser.
And you will immediately see the work of your bindings, triggers, how the display for certain data changes.
Note
This may fail on more complex VMs/packages, but in general by setting the DataContext at design time is not difficult.
I need to recompile the project to see the changes in the properties.
The XAML Designer panel has an «Enable/Disable Project Code» button.
, but during design phase in Visual Studio I see all of them, so I have problems in figuring the final layout.
This problem is easily resolved by bringing up the Document Outline tab in visual studio. Once open, navigate to the visible tree and toggle the eyeball to visibly hide/unhide the control[s] one is not interested in; during design time only.

MainWindow loaded but not visible at all time

I want to build a WPF Application that, when started, only has a tray Icon. If the User interacts with Menu Entries from the Tray's Context Menu, there will be Windows.
I need to load the MainWindow though, so I can listen to Clipboard Changed Events. But I don't want to show it.
I tried:
<Window x:Class="ClipboardListener.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:tb="http://www.hardcodet.net/taskbar"
Title="Should not see me"
ShowInTaskbar="False" Visibility="Collapsed" Opacity="100"
Width="320" Height="240">
But it still shows up? Setting Visibility to Hidden does not work for me since I need some Window to register the Clipboard Event Listener with the WinAPI.
Any Ideas?
I've recently had very similar task. All my attempts to make Window invisible, my googling, my stackoverflowing etc. failed. Finally I had a feeling that invisible window is something that should not be in WPF on some reason. It would be an easy task if there was TrayIcon control like in WinForms. Unfortunately, WPF does not have TrayIcon. This leads to the one that is present in WinForms.
Here's a good article on the issue. And my code that uses this dll:
<Window x:Class="ScannerClientWpf.TrayIcon"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ni="clr-namespace:Hardcodet.Wpf.TaskbarNotification;assembly=Hardcodet.Wpf.TaskbarNotification"
Title="TrayIcon" WindowStyle="None" AllowsTransparency="True" Background="Transparent" ShowActivated="False" ShowInTaskbar="False" >
<Grid>
<ni:TaskbarIcon IconSource="/ScannerClient;component/app.ico"
ToolTipText="ScannerClient">
<ni:TaskbarIcon.ContextMenu>
<ContextMenu>
<MenuItem Header="Close" Click="MenuItem_Click"/>
</ContextMenu>
</ni:TaskbarIcon.ContextMenu>
</ni:TaskbarIcon>
</Grid>

WPF: How to create events for custom usercontrols?

I have created a UserControl that consists of an ItemsCollection. Each item in this collection consists of a ListBox.
My application is represented by a Window, which contains this UserControl. I want to be able to manage events related to items inside the ListBox. How can I achieve this? (I am not sure if this is relevant or not, but the UserControl is in a assembly different from the application.)
Here's the code of the UserControl:
<UserControl
x:Class="UserControls.CalendarMonthViewControl.CalendarMonthView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignWidth="580"
d:DesignHeight="300"
xmlns:calendarMonthViewControl="clr-namespace:UserControls.CalendarMonthViewControl"
Name="CalendarMonthViewControl">
<Grid>
<ItemsControl
ItemsSource="{Binding ElementName=CalendarMonthViewControl, Path=Days}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Rows="6" Columns="7" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type calendarMonthViewControl:Day}">
<Border>
<Grid>
<ListBox ItemsSource="{Binding Path=CalendarDayItems}" />
</Grid>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</UserControl>
#vlad is on the right track.
There's a couple of options here. Both of them involve handling routed events.
To handle a routed event, you use the name of the owning class followed by the name of the event.
The first option is to simply handle the selection changed events (or some other ListBox event) on the Window class:
<Window ...
ListBox.SelectionChanged="OnChildListboxSelectionChanged">
...
</Window>
The second option (more typical approach) is to handle the ListBox events inside of the UserControl and then aggregate them in some way and fire an event at that level. This event is then handled by the Window. This event could be a routed event or a standard .NET event.
<UserControl ...
ListBox.SelectionChanged="OnChildListBoxSelectionChanged">
...
</UserControl>
Code behind for user control:
public event EventHandler<MyArgTypeEventArgs> ListBoxUpdated;
private void OnChildListBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
{
// aggregate data and info
MyArgTypeEventArgs handler = ListBoxUpdated;
if (handler != null)
handler.Invoke(this, GenerateArgs());
}
The Window handles this event:
<Window ...
ListBoxUpdated="OnListBoxUpdated">
...
</Window>
This should give you something to start with.
I haven't used them much myself, but I think RoutedEvents would solve your problem. The events bubble up from your ListBox to the Window (or another element lower in the tree) where you can handle them.
edit: quoting from the link: To add a handler for an event using XAML, you declare the event name as an attribute on the element that is an event listener. The value of the attribute is the name of your implemented handler method, which must exist in the partial class of the code-behind file.
since UserControl inherits from UIElement, I'm guessing something like this would work (untested atm):
<UserControl
x:Class="UserControls.CalendarMonthViewControl.CalendarMonthView"
[...]
ListBox.NameOfEvent="EventHandlerName">
I don't think what you're trying to do is the right approach as it's creating unnecessary dependence between your views and means that your UserControl is not properly encapsulated.
In my opinion the nice solution would be to handle events in your UserControl's viewmodel and set up a relation between the viewmodel of your Window as needed, so that the views themselves are independent.

Defining MenuItem Shortcuts

I need a simple way to set a shortcut for menu items.
But this don´t work with shortcut, just with click:
<MenuItem Header="Editar">
<MenuItem Header="Procurar" Name="MenuProcurar"
InputGestureText="Ctrl+F"
Click="MenuProcurar_Click">
<MenuItem.ToolTip>
<ToolTip>
Procurar
</ToolTip>
</MenuItem.ToolTip>
</MenuItem>
</MenuItem>
I am using WPF 4.0
H.B. was right... I just wanted to add more precisions.
Remove the Click event on your MenuItem and associate it with a Command instead.
1 - Add/create your commands:
<Window.CommandBindings>
<CommandBinding Command="Open" Executed="OpenCommandBinding_Executed"/>
<CommandBinding Command="SaveAs" Executed="SaveAsCommandBinding_Executed"/>
</Window.CommandBindings>
The commands are refering to the following code:
private void OpenCommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
Open();//Implementation of open file
}
private void SaveAsCommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
SaveAs();//Implementation of saveAs
}
2 - Associate the commands with the wanted keys:
<Window.InputBindings>
<KeyBinding Key="O" Modifiers="Control" Command="Open"/>
<KeyBinding Key="S" Modifiers="Control" Command="SaveAs"/>
</Window.InputBindings>
3 - Finally assign the commands with you menu item (InputGestureText is just a decorating text):
<Menu Name="menu1">
<MenuItem Header="_File">
<MenuItem Name="menuOpen" Header="_Open..." Command="Open" InputGestureText="Ctrl+O"/>
<MenuItem Name="menuSaveAs" Header="_Save as..." Command="SaveAs" InputGestureText="Ctrl+S"/>
</MenuItem>
</Menu>
That way multiple inputs may be associated to the same command.
You need to use KeyBindings (and CommandBindings if you (re)use RoutedCommands such as those found in the ApplicationCommands class) for that in the controls where the shortcuts should work.
e.g.
<Window.CommandBindings>
<CommandBinding Command="New" Executed="CommandBinding_Executed" />
</Window.CommandBindings>
<Window.InputBindings>
<KeyBinding Key="N" Modifiers="Control" Command="New"/>
</Window.InputBindings>
For custom RoutedCommands:
static class CustomCommands
{
public static RoutedCommand DoStuff = new RoutedCommand();
}
usage:
<Window
...
xmlns:local="clr-namespace:MyNamespace">
<Window.CommandBindings>
<CommandBinding Command="local:CustomCommands.DoStuff" Executed="DoStuff_Executed" />
</Window.CommandBindings>
<Window.InputBindings>
<KeyBinding Key="D" Modifiers="Control" Command="local:CustomCommands.DoStuff"/>
</Window.InputBindings>
...
</Window>
(It is often more convenient to implement the ICommand interface rather than using RoutedCommands. You can have a constructor which takes delegates for Execute and CanExecute to easily create commands which do different things, such implementations are often called DelegateCommand or RelayCommand. This way you do not need CommandBindings.)
In my humble opinion is much easier just to use _ at the Header. This will create automatically the desired HotKey.
For example:
<MenuItem Header="_Editar">
<MenuItem Header="_Procurar" Name="MenuProcurar"
InputGestureText="Ctrl+F"
Click="MenuProcurar_Click">
<MenuItem.ToolTip>
<ToolTip>
Procurar
</ToolTip>
</MenuItem.ToolTip>
</MenuItem>
</MenuItem>
I'm overly biased by Windows.Forms & gulp VB 6, so I kind of agree with Jonathan and Jase that there's got to be a more straightforward/procedural method to statically wire up event handlers that aren't necessarily CommandBindings. And there is, I think.
A good tutorial for using non-CommandBinding handlers like this, but with an emphasis on buttons, can be found in this MSDN blog post, I believe. I'll distill and target MenuItems...
Creating the ICommand
First, create a class implementing ICommand. You can put this anywhere, of course, even in your MainWindow.xaml.cs file if you wanted, to keep your demo code insanely simple. You'll probably want to make CanExecute more complicated when you want to dis/en/able menu items later, but for now, we'll just always have our menu items enabled.
public class HelloWorldCommand : ICommand
{
public void Execute(object parameter)
{
MessageBox.Show(#"""Hello, world!"" from "
+ (parameter ?? "somewhere secret").ToString());
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
}
As the tutorial helpfully points out, you could call this command from anywhere already, with code like...
var hwc = new HelloWorldCommand();
if (hwc.CanExecute(this))
hwc.Execute(this);
Declaring your command in the Window
So let's add a sort of "declaration" for the HelloWorldCommand to our Window so that we can use it later. Inside of your Window tags, register the command as a resource:
<Window.Resources>
<local:HelloWorldCommand x:Key="hwc"/>
</Window.Resources>
Now we have a neat shortcut for linking to this "locally namespaced" command, "hwc", though you can obviously use any string you want. We'll use that a lot in our xaml.
Wiring up (and reusing!) the command
Let's add our MenuItems to our xaml. I've replaced the stock Grid with a DockPanel because that's the easiest way (for me) to have equi-spaced widgets that fill the Window, though I've left all of the rest of my UI out.
Note the Command="{StaticResource hwc}"s sprinkled into each MenuItem declaration. The key is the hwc in there - remember that that's our shortcut for the HelloWorldCommand that we set up at the Window level. And, of course, StaticResource says just to look it up the Window's resources. We're not binding anything; we're just using our shortcut.
<DockPanel LastChildFill="True">
<Menu DockPanel.Dock="Top">
<MenuItem Header="_File">
<MenuItem
Header="_Open"
Command="{StaticResource hwc}"
>
<MenuItem.CommandParameter>
<!-- so you could make this object as complex as you wanted,
like, say, your entire Window. See magic incantation, below. -->
<Binding RelativeSource="{RelativeSource FindAncestor, AncestorType=Window}" />
</MenuItem.CommandParameter>
</MenuItem>
<MenuItem
Header="_Close"
Command="{StaticResource hwc}"
CommandParameter="Close"
InputGestureText="Ctrl+G" />
<MenuItem
Header="_Save"
Command="{StaticResource hwc}"
CommandParameter="Save" />
<Separator />
<MenuItem
Header="_Quit"
Command="{StaticResource hwc}"
CommandParameter="Quit" />
</MenuItem>
</DockPanel>
CommandParameters to distinguish event sources
Note that we're using the same Command for everything! But how can we tell which widget threw the event? For that, you need to use the CommandParameter -- remember our Execute method's signature: Execute(object parameter). That CommandParameter parameter is what we can use to know how to handle the event. Try running this and note that the MessageBox will use whatever's in CommandParameter to let you know the source of the event. We're doing it all manually, but that's not too bad.
Also note that you can make these objects as complicated as you'd like. You can use a property in the MenuItem tag to define the parameter, or can use "real" <MenuItem.CommandParameter> tags, like in the Open menu item, above, to define something complex. In this case, we're passing the entire parent Window object, which was the easiest (though not the cleanest) way to throw our VB6-ish context into the event handler code.
Adding keyboard shortcuts to MenuItems (aka, "Answering the OP")
And now we can finally answer the original question! Let's finally wire up a keyboard shortcut for the Close menu item. You'll note that we've already declared an InputGestureText. By itself, InputGestureText is only cosmetic. If we were overly picky, we could argue the mechanism for creating the keyboard shortcut doesn't have any direct, inherent relationship with the MenuItem at all!
We need to instead (or additionally) register a listener for Ctrl-G at the Window level to catch the keystroke. So at the top level of your Window tags, insert this (taken essentially from here):
<Window.InputBindings>
<KeyBinding Modifiers="Control"
Key="G"
Command="{StaticResource hwc}"
CommandParameter="window input binding"
/>
</Window.InputBindings>
Note that you could put CommandParameter tags in your KeyBinding by moving it from a self-closing piece of XML to "real" open and close KeyBinding tags.
And we're done. Run your app, and press Ctrl-G. Whaddup.
Pretty straightforward, once you've got the players straight, and much less magic binding-y than most intros to commands and MenuItems, I think.
Possible pro tip:
The whole CommandBinding thing confused me for a while. This is just for specific command types, I believe. That is, you can't just wire up any Command you like. Stuff like what's bragged about here (in what's admittedly a decent intro tutorial!)...
It might not be completely obvious, but by using commands, we just got a whole bunch of things for free: Keyboard shortcuts, text and InputGestureText on the items and WPF automatically enables/disables the items depending on the active control and its state. In this case, Cut and Copy are disabled because no text is selected, but Paste is enabled, because my clipboard is not empty!
... is kind of magic-y and not necessarily good, and can be confusing when you're new to WPF menus.
You can also declare RoutedUICommand in XAML:
<Window.Resources>
<RoutedUICommand x:Key="BuildCmd" Text="Build">
<RoutedUICommand.InputGestures>
<KeyGesture>CTRL+SHIFT+B</KeyGesture>
</RoutedUICommand.InputGestures>
</RoutedUICommand>
</Window.Resources>
Do the binding
<Window.CommandBindings>
<CommandBinding Command="{StaticResource BuildCmd}" Executed="BuildCmdExecuted"/>
</Window.CommandBindings>
And in MenuItem
<MenuItem Command="{StaticResource BuildCmd}"/>
Another solution is discussed here.
Here is solution in PowerShell:
Define your XAML file:
<Window x:Class="WpfApp1.Window1"
xmlns:local="clr-namespace:WpfApp1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="My App" Height="620" Width="950" >
<Window.InputBindings>
<KeyBinding Key="S" Modifiers="Ctrl"/>
</Window.InputBindings>
<Grid x:Name="MainGrid">
<!--Your GUI is here-->
<Menu Margin="0">
<MenuItem Header="_File">
<MenuItem x:Name="SaveProfile" Header="_Save Profile" InputGestureText="Ctrl+S"/>
</MenuItem>
</Menu>
</Grid>
</Window>
Add new type to be able to create Command(s)
Add-Type #"
public class DelegateCommand : System.Windows.Input.ICommand
{
private System.Action<object> _action;
public DelegateCommand(System.Action<object> action)
{
_action = action;
}
public bool CanExecute(object parameter)
{
return true;
}
public event System.EventHandler CanExecuteChanged = delegate { };
public void Execute(object parameter)
{
_action(parameter);
}
}
"#
Create and assign new Command to the previously defined KeyBinding, it is first key binding, that is why I'm addressing it with [0].
Note:in my case I saved the handle to the main Window in $hash.Window variable, you should put here the link to your main window object, that you created with [Windows.Markup.XamlReader]::Load($xamlXmlNodeReader) command or other window creation command.
$hash.Window.InputBindings[0].Command = New-Object DelegateCommand( { Save-Profile } )
Create your function that you put into command
function Save-Profile {
Write-Host "Save Profile"
# Your logic goes here
}
Thanks to Nicholas Wolverson for the tips on how to create the Type for Command.
This work for me
<ContextMenu PreviewKeyUp="ContextMenu_PreviewKeyUp">
<MenuItem Header="Delete" Click="DeleteID" />
</ContextMenu>
Code behind:
private void ContextMenu_PreviewKeyUp(object sender, KeyEventArgs e)
{
ContextMenu contextMenu = sender as ContextMenu;
if (e.Key == Key.D)
{
//DELETE ID
}
contextMenu.IsOpen = false;
}

Can I have multiple CommandBindings for the same Command on the same control?

I have a UserControl that adds a CommandBinding to it's CommandBindings collection to handle a specific Command. Later I use this control in a window and want to add another binding to that same control to add additional behavior. The problem though, is that when I do this it seems that when I add another CommandBinding to the CommandBindings collection of a control that it replaces any binding that was already made for the same Command. So what it seems like is that a control can only have a single CommandBinding per control, is this correct?
Please see the code example below which attempts to set two CommandBindings for the same Save Command.
<Window x:Class="MultipleCommandBindings.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.CommandBindings>
<CommandBinding Command="Save"
Executed="CommandBinding_Executed" />
<CommandBinding Command="Save"
Executed="CommandBinding_Executed" />
</Window.CommandBindings>
<Grid>
<Button Height="23"
HorizontalAlignment="Right"
Margin="0,0,25,88"
Name="button1"
VerticalAlignment="Bottom"
Width="75"
Command="Save">Button</Button>
</Grid>
Originally I was expecting either a compile-time or runtime exception when wrote this code but was surprised that it didn't complain. Next though I was disappointed since my CommandBinding_Executed handler only gets called once instead of twice as I was hoping.
Update:
After a bit of testing it appears that my second CommandBinding is not overwriting my first one but instead it appears that even though I'm not setting Handled to true in my event handler that the first command binding swallows up the Command. I'm pretty sure at this point that the solution to my problem is to understand why the routed command is not propagating past the first handler even when Handled is not set to true.
Update:
I've found this great little tidbit of information which just confirms some of the strange behavior behind Command routing in WPF.
Update:
One thought about how to work around the fact that it appears that there can only be a single effective CommandBinding per command is that it appears that the default CommandBinding class exposes Executed and CanExecute as events which of course like all events can have multiple handlers. The idea then is to have some other way than the standard CommandBindings.Add method to add additional handlers to a command. Maybe this could be done via an extension method on the Control class and conjunction with a custom CompositeCommandBinding class which allows us to aggregate multiple bindings within one main binding.
So far I've only been able to come up with a workaround for this problem which is to handle the command at two different levels in the logical tree. In the example below I handle the Save command within my grid and then also again within the Window element.
<Window x:Class="MultipleCommandBindings.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.CommandBindings>
<CommandBinding Command="Save"
Executed="CommandBinding_Executed2"/>
</Window.CommandBindings>
<Grid>
<Grid.CommandBindings>
<CommandBinding Command="Save"
Executed="CommandBinding_Executed1" />
</Grid.CommandBindings>
<Button Height="23"
HorizontalAlignment="Right"
Margin="0,0,25,88"
Name="button1"
VerticalAlignment="Bottom"
Width="75"
Command="Save">Button</Button>
</Grid>
In order to get this to work my code behind needs to also manually propagate the Command execution to the next level up.
private void CommandBinding_Executed1(object sender, ExecutedRoutedEventArgs e)
{
System.Diagnostics.Debug.WriteLine("Executed 1!");
var command = e.Command as RoutedUICommand;
command.Execute(e.Parameter, this);
}
private void CommandBinding_Executed2(object sender, ExecutedRoutedEventArgs e)
{
System.Diagnostics.Debug.WriteLine("Executed 2!");
}
If anybody has any better ideas on how I can monitor a command still let it propagate naturally up the tree I would love to see it otherwise I might just resort to this workaround.
I don't know the answer to your question, but using Reflector sounds reasonable to me.
I'm wondering why are you doing this? Maybe it makes more sense to use Composite pattern to aggregate behaviors, rather then trying to combine command bindings?

Resources