I was doing some test with CommandBindings modifying some code found in a book and I stumbled upon a problem.
I'd like to define a custom behaviour for the cut/copy/paste commands working both with the usual key combinations and context menu in a textbox and from the standard menu. In order to avoid redefining the CommandBindings I declared them as window resources. All is working good in the textbox but I don't know how to use those in the menuitems.
This is the code I put in place till now:
<Window x:Class="MySpellChecker.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MySpellChecker" Height="331" Width="508"
WindowStartupLocation ="CenterScreen" Focusable="True">
<Window.Resources>
<CommandBinding Command="ApplicationCommands.Open"
x:Key="OpenCmdBinding"
Executed="OpenCmdExecuted"
CanExecute="OpenCmdCanExecute"/>
<CommandBinding Command="ApplicationCommands.Save"
x:Key="SaveCmdBinding"
Executed="SaveCmdExecuted"
CanExecute="SaveCmdCanExecute"/>
<CommandBinding Command="ApplicationCommands.Copy"
x:Key="CopyCmdBinding"
Executed="ClipboardCmdExecuted"
CanExecute="ClipboardCmdCanExecute"/>
<CommandBinding Command="ApplicationCommands.Cut"
x:Key="CutCmdBinding"
Executed="ClipboardCmdExecuted"
CanExecute="ClipboardCmdCanExecute"/>
<CommandBinding Command="ApplicationCommands.Paste"
x:Key="PasteCmdBinding"
Executed="ClipboardCmdExecuted"
CanExecute="ClipboardCmdCanExecute"/>
</Window.Resources>
<DockPanel>
<!--Doc menu system on the top-->
<Menu DockPanel.Dock ="Top"
HorizontalAlignment="Left" Background="White" BorderBrush ="Black">
<MenuItem Header="_File" >
<MenuItem Command ="ApplicationCommands.Open"/>
<MenuItem Command ="ApplicationCommands.Save"/>
<Separator/>
<MenuItem Header ="_Exit"
MouseEnter ="MouseEnterExitArea"
MouseLeave ="MouseLeaveArea"
Click ="FileExit_Click"/>
</MenuItem>
<MenuItem Header="_Edit">
<MenuItem Command ="ApplicationCommands.Copy"/>
<MenuItem Command ="ApplicationCommands.Cut"/>
<MenuItem Command ="ApplicationCommands.Paste"/>
</MenuItem>
<MenuItem Header="_Tools">
<MenuItem Header ="_Spelling Hints"
MouseEnter ="MouseEnterToolsHintsArea"
MouseLeave ="MouseLeaveArea"
Click ="ToolsSpellingHints_Click"/>
</MenuItem>
</Menu>
<!-- Put Toolbar under the Menu -->
<ToolBar DockPanel.Dock ="Top" >
<Button Content ="Exit" MouseEnter ="MouseEnterExitArea"
MouseLeave ="MouseLeaveArea" Click ="FileExit_Click"/>
<Separator/>
<Button Content ="Check" MouseEnter ="MouseEnterToolsHintsArea"
MouseLeave ="MouseLeaveArea" Click ="ToolsSpellingHints_Click"
Cursor="Help" />
</ToolBar>
<!-- Put a StatusBar at the bottom -->
<StatusBar DockPanel.Dock ="Bottom"
Background="Beige" >
<StatusBarItem>
<TextBlock Name="statBarText" Text ="Ready"/>
</StatusBarItem>
</StatusBar>
<Grid DockPanel.Dock ="Left" Background ="AliceBlue">
<!-- Define the rows and columns -->
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<GridSplitter Grid.Column ="0" Width ="5" Background ="Gray" />
<StackPanel Grid.Column="0" VerticalAlignment ="Stretch" >
<Label Name="lblSpellingInstructions"
FontSize="14" Margin="10,10,0,0">Spelling Hints</Label>
<Expander Name="expanderSpelling" Header ="Try these!" Margin="10,10,10,10">
<!-- This will be filled programmatically -->
<Label Name ="lblSpellingHints" FontSize ="12"/>
</Expander>
</StackPanel>
<!-- This will be the area to type within -->
<TextBox Grid.Column ="1"
SpellCheck.IsEnabled ="True"
AcceptsReturn ="True"
Name ="txtData" FontSize ="14"
BorderBrush ="Blue" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
<TextBox.CommandBindings>
<StaticResource ResourceKey="CutCmdBinding"/>
<StaticResource ResourceKey="CopyCmdBinding"/>
<StaticResource ResourceKey="PasteCmdBinding"/>
</TextBox.CommandBindings>
</TextBox>
</Grid>
</DockPanel>
In this situation if I wrote something in the textbox I can use cut/copy/paste from keyboard shortcuts, context menu and curiously enough from the main menu. Even though something is clearly not working well since before I write on the textbox all the Edit menu items are disables and, more than that, the File menu items never get to work. They work if I declare the commandbindings in Window.Commandbindings but then again I'm trying to understand if it's possibile to define the commands and their behaviour just once and then reuse them all through the application no matter what the control using them.
Any idea? Thanks in advance!