How to set WPF ApplicationCommands in code behind - c#

I know how to set default ApplicationCommands commands in WPF in order to enable simple cut, copy, and paste operations via ContextMenu. However I need to be able to do this in the code behind so that I can assign the commands dynamically as my TextBoxes are created.
How can I recreate this very simple WPF code in the code behind:
<TextBox x:Name="txtTagName" Style="{StaticResource TextBoxStyle}">
<TextBox.ContextMenu>
<ContextMenu Style="{StaticResource DefaultContextMenuStyle}">
<MenuItem x:Name="cmCut" Header="Cut" Command="ApplicationCommands.Cut" />
<MenuItem x:Name="cmCopy" Header="Copy" Command="ApplicationCommands.Copy" />
<MenuItem x:Name="cmPaste" Header="Paste" Command="ApplicationCommands.Paste" />
</ContextMenu>
</TextBox.ContextMenu>
</TextBox>

you could do something like:
this.cmCut.Command = ApplicationCommands.Cut;

How can I recreate this very simple WPF code in the code behind
Something like this, i.e. you programmatically create an instance of a TextBox and a ContextMenu and set the same properties that you set in your XAML markup:
TextBox textBox = new TextBox();
textBox.Style = FindResource("TextBoxStyle") as Style;
ContextMenu cm = new ContextMenu();
cm.Style = FindResource("DefaultContextMenuStyle") as Style;
cm.Items.Add(new MenuItem() { Header = "Cut", Command = ApplicationCommands.Cut });
cm.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy });
cm.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste });
textBox.ContextMenu = cm;

Good answer found here: How do I add a custom routed command in WPF?
I wanted to add custom inputs with my own commands for MenuItems and with appropriate texts for the commands displayed in the MenuItems. What solved my problem was to add both a command binding and an input binding section for the window there I could bind my command class and input to that command:
<Window x:Class="SomeNamespace.MainWindow"
<!--Other stuff here-->
xmlns:local="clr-namespace:SomeNamespace"
mc:Ignorable="d"
Title="MainWindow" Height="544" Width="800">
<Window.CommandBindings>
<CommandBinding Command="local:Commands.SomeCommand" Executed="CommandBinding_SomeCommand" />
<CommandBinding Command="local:Commands.SomeOtherCommand" Executed="CommandBinding_SomeOtherCommand" />
</Window.CommandBindings>
<Window.InputBindings>
<KeyBinding Command="local:Commands.SomeCommand" Key="S" Modifiers="Ctrl" />
<KeyBinding Command="local:Commands.SomeOtherCommand" Key="O" Modifiers="Ctrl" />
</Window.InputBindings>
And then I could use it in my MenuItems like this (note that "InputGestureText" adds the shortcut/input text to the MenuItem):
<MenuItem Name="MenuItemSomeCommand" Command="local:Commands.SomeCommand" InputGestureText="Ctrl+S" />
<MenuItem Name="MenuItemSomeOtherCommand" Command="local:Commands.SomeOtherCommand" InputGestureText="Ctrl+O" />
Code for the "Commands" class (in my case in Commands.cs):
using System.Windows.Input;
namespace SomeNamespace
{
public static class Commands
{
public static readonly RoutedUICommand BuildFiles =
new RoutedUICommand("Some Command", "SomeCommand", typeof(MainWindow));
public static readonly RoutedUICommand BuildFiles =
new RoutedUICommand("Some Other Command", "SomeOtherCommand", typeof(MainWindow));
}
}
And code for the Executed binding command in MainWindow.xaml.cs:
public void CommandBinding_SomeCommand(Object sender, ExecutedRoutedEventArgs e)
{
// Add code that should trigger when the "SomeCommand" MenuItem is pressed
}
public void CommandBinding_SomeOtherCommand(Object sender, ExecutedRoutedEventArgs e)
{
// Add code that should trigger when the "SomeOtherCommand" MenuItem is pressed
}

Related

Why this simple WPF command does not work?

I'm trying to add a WPF custom command to a control. What I've done:
XAML:
<Window x:Class="H.I.S.windows.CommandTest"
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:H.I.S.windows"
mc:Ignorable="d"
Title="CommandTest" Height="450" Width="800">
<Window.CommandBindings>
<CommandBinding Command="local:CustomCommand.Save" CanExecute ="SaveCommand_CanExecute" Executed="SaveCommand_Executed" />
</Window.CommandBindings>
<Grid>
<Button Command="local:CustomCommand.Save" Height="50" Width="100">Click me!</Button>
</Grid>
</Window>
C#:
namespace H.I.S.windows
{
public partial class CommandTest : Window
{
public CommandTest()
{
InitializeComponent();
}
private void SaveCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
private void SaveCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Saved");
}
}
public static class CustomCommand
{
public static readonly RoutedUICommand Save = new RoutedUICommand(
"Save",
"Save",
typeof(CustomCommand),
new InputGestureCollection()
{
new KeyGesture(Key.F2)
}
);
}
}
The button is disabled (even in design mode) and doesn't let the user click on it.
I've just implemented codes described HERE.
Where I'm wrong?
The code what you have posted throws me an error because of this below statement,
<Window.CommandBindings>
<CommandBinding Command="local:CustomCommand.Save" CanExecute ="CommandBinding_CanExecute" Executed="CommandBinding_Executed" />
</Window.CommandBindings>
It started working for me after changing it to below,
<Window.CommandBindings>
<CommandBinding Command="local:CustomCommand.Save" CanExecute ="SaveCommand_CanExecute" Executed="SaveCommand_Executed" />
</Window.CommandBindings>
The event handlers in your code-behind are different from what you have in your xaml for CommandBinding.
"SaveCommand_CanExecute" and "SaveCommand_Executed"
After changing as above, it works for me and I can see the messagebox with "Saved" message when I hit click on it.
Hope you are not missing this. If something else stopping you then try to see if it shows any errors on further and let us know.
As #SirRufo suggested in question comments, Problem was that I declared "CommandBindings" for whole WPF window and button inside another control.
There are 2 solutions for this case:
1: Declaring CommandBindings for Button's direct parent.
2: Set name for control which is bound with command and add some code like below to control:
<Window ... x:Name = "windowName" ...>
<Window.CommandBindings>
<CommandBinding Command="custumCommand:CustomCommands.Save" CanExecute ="CommandBinding_CanExecute" Executed="CommandBinding_Executed" />
</Window.CommandBindings>
<Grid>
<StackPanel>
<GroupBox>
<Button Command="custumCommand:CustomCommands.Save" CommandTarget = "{Binding ElementName=windowName}" Content="Save" />
</GroupBox>
</StackPanel>
</Grid>
</Window>
Look at "CommandTarget" attribute of Button.

WPF EditingCommands Command Binding

The Situation:
I have some Editing commands in WPF window, and a close command (Application.CloseCommand) and have some bindings like this
View:
<Window.CommandBindings>
<CommandBinding Command="ApplicationCommands.Close"
Executed="CloseCommandBinding_Executed"/>
<CommandBinding Command="EditingCommands.ToggleBold"
Executed="EditingCommand_Executed"></CommandBinding>
</Window.CommandBindings>
<Window.InputBindings>
<KeyBinding Key="Esc" Command="ApplicationCommands.Close"></KeyBinding>
</Window.InputBindings>
.. *Some panel and grid stuff and more things* ..
<RichTextBox Name="RTBPopup">
<RichTextBox.InputBindings>
<KeyBinding Key="Esc" Command="ApplicationCommands.Close"></KeyBinding>
</RichTextBox.InputBindings>
</RichTextBox>
.. *Some panel and grid stuff and more things* ..
<ToggleButton x:Name="btnToggleBold" CommandManager.Executed="EditingCommand_Executed" Command="EditingCommands.ToggleBold" CommandTarget="{Binding ElementName=RTBPopup}">B</ToggleButton>
Now:
If I press escape in RTBPopup (Richtextbox) the command gets executed and the debugger hits the breakpoint set on CloseCommandBinding_Executed method
but
when I click on toggle button for bold or press control + B, the EditingCommand_Executed is not getting hit by debugger (not getting executed)
What else I have tried:
<ToggleButton.CommandBindings>
<CommandBinding Command="EditingCommands.ToggleBold" Executed="EditingCommand_Executed"></CommandBinding>
</ToggleButton.CommandBindings>
Handle the PreviewExecuted event:
<CommandBinding Command="EditingCommands.ToggleBold"
PreviewExecuted="CommandBinding_PreviewExecuted" />
The command is handled by the RichTextBox so it never bubbles up to your parent Window.
You could also try to use the CommandManager to hook up the event handler programmatically:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
CommandManager.AddPreviewExecutedHandler(RTBPopup, new ExecutedRoutedEventHandler(OnExecuted));
}
private void OnExecuted(object sender, ExecutedRoutedEventArgs e)
{
if(e.Command == EditingCommands.ToggleBold)
{
MessageBox.Show("fired!");
}
}
}

How to make a RichTextBox stop consuming key gestures

The RichTextBox provides a lot of commands with accompanying key gestures.
However, when IsReadOnly=True is set, many of those commands do not make sense and do not take any action, e.g. Ctrl+I, Ctrl+R. Nevertheless, when a command in the Window uses one of the key gestures, the command in the Window does not fire when the RichtTextBox has focus:
<Window x:Class="MainWindow"
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:"
mc:Ignorable="d">
<Window.CommandBindings>
<CommandBinding Command="local:MainWindow.MyCommand" Executed="MyCommandBinding_Executed" />
</Window.CommandBindings>
<StackPanel>
<RichTextBox Height="50" IsReadOnly="True" />
<CheckBox Content="This is a checkbox" />
<TextBlock Name="whatsGoingOn" />
</StackPanel>
</Window>
using System.Windows;
using System.Windows.Input;
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public static RoutedUICommand MyCommand = new RoutedUICommand(
"My Command",
"MyCommand",
typeof(MainWindow),
new InputGestureCollection() { new KeyGesture(Key.R, ModifierKeys.Control) });
private void MyCommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
whatsGoingOn.Text += ".";
}
}
Now maybe the right answer is “don’t use RichTextBox with IsReadOnly="True", use something else instead”, but let’s assume I had to or wanted to use RichTextBox.
What can I do to make key gestures fire my command even when the RichTextBox has focus?
You can intercept the keystrokes in the PreviewKeyDown event of the RichTextBox. Set the e.Handled member to true and that will prevent the actually processing of the keys.
Hope it helps

WPF ComamndBinding Help on MenuItem

New to WPF...was reading this WPF Routed Command with Bindings per-Tab and am close to getting it working.
The MenuItem is disabled until my RuleTab (tabitem) is selected but rather than pop my Find Dialog it shows System.Windows.Input.CommandBinding on the menu. What am I doing wrong?
XAML:
<MenuItem Header="_Find..." IsEnabled="{Binding ElementName=RuleTab, Path=IsSelected}" >
<CommandBinding Command="Find" Executed="ExecuteFind" CanExecute="Find_CanExecute" ></CommandBinding>
</MenuItem>
Code-Behind:
private void ExecuteFind(object sender, ExecutedRoutedEventArgs e)
{
// Initiate FindDialog
FindDialog dlg = new FindDialog(this.RuleText);
// Configure the dialog box
dlg.Owner = this;
dlg.TextFound += new TextFoundEventHandler(dlg_TextFound);
// Open the dialog box modally
dlg.Show();
}
void dlg_TextFound(object sender, EventArgs e)
{
// Get the find dialog box that raised the event
FindDialog dlg = (FindDialog)sender;
// Get find results and select found text
this.RuleText.Select(dlg.Index, dlg.Length);
this.RuleText.Focus();
}
private void Find_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = RuleTab.IsSelected;
}
Any suggestions would be greatly appreciated!
Figured it out! Thanks to those responded. All I had to do was move my commandbinding to:
<Window.CommandBindings>
<CommandBinding Command="Find" Executed="ExecuteFind" CanExecute="Find_CanExecute" ></CommandBinding>
</Window.CommandBindings>
Then reference Command=Find in my MenuItem.
You'll find you need to add the CommandBinding to the TabItem (as per the linked sample). Then to bind your MenuItem you should use the Command property, possibly along with a CommandParameter and CommandTarget (pointing back at the TabItem I would expect).
For example, I have a MenuItem in a ContextMenu and I want the command to fire on the context (placement target) of the ContextMenu:
<MenuItem Header="View"
ToolTip="Open the Member Central view for this member"
Command="{x:Static local:Commands.CustomerViewed}"
CommandParameter="{Binding Path=PlacementTarget.DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}"
CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}"
/>

How to reference right-clicked object in WPF Context Menu item click event handler?

In WPF application there is a Grid with a number of objects (they are derived from a custom control). I want to perform some actions on each of them using context menu:
<Grid.ContextMenu>
<ContextMenu>
<MenuItem Name="EditStatusCm" Header="Change status" Click="EditStatusCm_Click"/>
</ContextMenu>
</Grid.ContextMenu>
But in the event handler I cannot get know which of the objects was right-clicked:
private void EditStatusCm_Click(object sender, RoutedEventArgs e)
{
MyCustControl SCurrent = new MyCustControl();
MenuItem menu = sender as MenuItem;
SCurrent = menu.DataContext as MyCustControl; // here I get a run-time error
SCurrent.Status = MyCustControl.Status.Sixth;
}
On that commented line Debugger says: Object reference not set to an instance of an object.
Please help, what is wrong in my code?
Edited (added):
I tried to do the same, using Command approach:
I declared a DataCommands Class with RoutedUICommand Requery and then used Window.CommandBindings
<Window.CommandBindings>
<CommandBinding Command="MyNamespace:DataCommands.Requery" Executed="RequeryCommand_Executed"></CommandBinding>
</Window.CommandBindings>
XAML of MenuItem now looks like:
<Grid.ContextMenu>
<ContextMenu>
<MenuItem Name="EditStatusCm" Header="Change status" Command="MyNamespace:DataCommands.Requery"/>
</ContextMenu>
</Grid.ContextMenu>
And event handler looks like:
private void RequeryCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
IInputElement parent = (IInputElement)LogicalTreeHelper.GetParent((DependencyObject)sender);
MyCustControl SCurrent = new MyCustControl();
SCurrent = (MuCustControl)parent;
string str = SCurrent.Name.ToString();// here I get the same error
MessageBox.Show(str);
}
But debugger shows the same run-time error: Object reference not set to an instance of an object.
What is missing in my both approaches?
How I should reference right-clicked object in WPF Context Menu item click event handler?
note the CommandParameter
<Grid Background="Red" Height="100" Width="100">
<Grid.ContextMenu>
<ContextMenu>
<MenuItem
Header="Change status"
Click="EditStatusCm_Click"
CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Parent}" />
</ContextMenu>
</Grid.ContextMenu>
</Grid>
and use it in the handler to figure out which Grid it is
private void EditStatusCm_Click(object sender, RoutedEventArgs e)
{
MenuItem mi = sender as MenuItem;
if (mi != null)
{
ContextMenu cm = mi.CommandParameter as ContextMenu;
if (cm != null)
{
Grid g = cm.PlacementTarget as Grid;
if (g != null)
{
Console.WriteLine(g.Background); // Will print red
}
}
}
}
Update:
If you want the menuitem handler to get to the Grid's children instead of the Grid itself, use this approach
<Grid Background="Red" Height="100" Width="100">
<Grid.Resources>
<ContextMenu x:Key="TextBlockContextMenu">
<MenuItem
Header="Change status"
Click="EditStatusCm_Click"
CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Parent}" />
</ContextMenu>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="ContextMenu" Value="{StaticResource TextBlockContextMenu}" />
</Style>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Text="Row0" Grid.Row="0" />
<TextBlock Text="Row1" Grid.Row="1" />
</Grid>
Just replace the TextBlocks with whatever your custom object type is. Then in the event handler, replace Grid g = cm.PlacementTarget as Grid with TextBlock t = cm.PlacementTarget as TextBlock (or whatever your custom object type is).
By binding the Data Context like so in the xaml:
ContextMenu DataContext="{Binding PlacementTarget.DataContext, RelativeSource= {RelativeSource Self}}">
You can then do this:
private void Context_MenuClick(object sender, RoutedEventArgs e)
{
var menuItem = e.Source as MenuItem;
MyDoStuffFunction(menuItem.DataContext);
}
The data context will be bound to the object that was clicked to open the ContextMenu.
I got it from a codeproject article at this link:
http://www.codeproject.com/Articles/162784/WPF-ContextMenu-Strikes-Again-DataContext-Not-Upda
menu = sender as MenuItem will be null if the sender is not a MenuItem or a derived class thereof. Subsequently trying to dereference menu will blow up.
It's likely that your sender is a Menu or ContextMenu or a ToolStripMenuItem or some other form of menu item, rather than specifically being a MenuItem object. Use a debugger breakpoint to stop the code here and examine the sender object to see exactly what class it is.
For RoutedEventArgs
RoutedEventArgs.source is the reference to the object that raised the event
RoutedEventArgs.originalSource is the reporting source as determined by pure hit testing, before any possible Source adjustment by a parent class.
So .Sender should be the answer. But this depends on how the menuitems are added and bound
See this answer collection and choose the method that will work for you situation!
Shouldn't you be checking RoutedEventArgs.Source instead of sender?
You had two different problems. Both problems resulted in the same exception, but were otherwise unrelated:
First problem
In your first approach your code was correct and ran well except for the problem here:
SCurrent.Status = MyCustControl.Status.Sixth;
The name "Status" is used both as a static member and as an instance member. I think you cut-and-pasted the code incorrectly into your question.
It may also be necessary to add the following after MenuItem menu = sender as MenuItem;, depending on your exact situation:
if(menu==null) return;
Second problem
In your second approach you used "sender" instead of "e.Source". The following code works as desired:
private void RequeryCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
IInputElement parent = (IInputElement)LogicalTreeHelper.GetParent((DependencyObject)e.Source);
// Changed "sender" to "e.Source" in the line above
MyCustControl SCurrent = new MyCustControl();
SCurrent = (MuCustControl)parent;
string str = SCurrent.Name.ToString();// Error gone
MessageBox.Show(str);
}
Final Note
Note: There is no reason at all to bind CommandParameter for this if you use the commanding approach. It is significantly slower and takes more code. e.Source will always be the source object so there is no need to use CommandParameter, so use that instead.
This works for me:-
XAML:-
<DataGrid.ContextMenu>
<ContextMenu x:Name="AddColumnsContextMenu" MenuItem.Click="AddColumnsContextMenu_Click">
</ContextMenu>
For adding menu items:-
foreach (String s in columnNames)
{
var item = new MenuItem { IsCheckable = true, IsChecked = true ,Header=s};
AddColumnsContextMenu.Items.Add(item);
}
And here comes the listener:-
private void AddColumnsContextMenu_Click(object sender, RoutedEventArgs e)
{
MenuItem mi = e.Source as MenuItem;
string title = mi.Header.ToString();
MessageBox.Show("Selected"+title);
}
Thanks...
In my case I was able to use:
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
MenuItem menuItem = e.Source as MenuItem;
ContextMenu parent = menuItem.Parent as ContextMenu;
ListBoxItem selectedItem = parent.PlacementTarget as ListBoxItem;
}

Categories