WPF .NET 4.6
In the below code, clicking on the menu item will activate the command and correctly display:
"InkAndGesture command executed"
It is my understanding that the RoutedUICommand will travel up and down the visual tree. So how can the ProgressNoteEditor (a custom control contained within the ItemsControl) listen and act upon the custom command? (There are many instantiations of the ProgressNoteEditor) ???
Note: I need ALL instances of ProgressNoteEditor to respond, not just one, so CommandTarget is no use. Do commands only bubble up?
TIA.
I have a CustomControl (ProgressNoteEditor) which is used from the MainWindow as:
<ItemsControl x:Name="ProgressNote" Grid.Column="1" Grid.Row="1" ItemsSource="{Binding WritingLayer.ProgressNote}" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<pn:ProgressNoteEditor LineCount="{Binding LineCount}"
Background="{Binding Background}"
Vocabulary="{Binding Vocabulary}"
/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
From the Menu in the MainWindow, I have added a custom command as:
<MenuItem Header="Ink And Getsures" Command="pn:NotePadCommands.InkAndGesture"/>
The code-behind does:
private void NewProgressNoteView_Loaded(object sender, RoutedEventArgs e)
{
CommandBindings.Add(
new CommandBinding(NotePadCommands.InkAndGesture, NotePadCommands.InkAndGesture_Executed, NotePadCommands.InkAndGesture_CanExecute));
}
For the moment, the CustomCommand is defined in its own class as:
namespace NotePad
{
public static class NotePadCommands
{
// Allow InkCanvas controls to use Gestures with Ink.
private static RoutedUICommand _InkAndGesture;
static NotePadCommands()
{
_InkAndGesture = new RoutedUICommand("Allow Gestures with Ink","InkAndGesture", typeof(NotePadCommands));
}
// Command: InkAndGesture
public static RoutedUICommand InkAndGesture
{
get { return _InkAndGesture; }
}
public static void InkAndGesture_Executed(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("InkAndGesture command executed");
}
public static void InkAndGesture_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
}
}
Do commands only bubble up?
A RoutedCommand
searches the visual tree from the focused element and up for an element that has a matching CommandBinding
and then executes the Execute
delegate for this particular CommandBinding
.
So the CommandBinding
of your ProgressNoteEditor
element won't be found the MenuItem
invokes the command, because it is not a visual ancestor of the MenuItem
.