CanExecute for KeyBindings to control - wpf

I have a reportViewer with default toolbar button for decrease zoom binded to command NavigationCommands.DecreaseZoom. I want to disable it in some situation so i bind CanExecute method to return false for that command which works perfectly fine and disable the button as expected. But, still zoom out works if i use shortcut key "Ctrl + Subtract key". I tried to set KeyBinding to the same command assuming CanExecute will work but it doesn't. Since, CanExecute is not provided in KeyBinding. Can someone suggest how can i disable KeyGesture "Ctrl -" for some situation(logic in CanExecute) and not permanently.
Relevant code -
<DocumentViewer Name="documentViewer1"
Margin="0,0,0,30"
Style="{DynamicResource DocumentViewerStyle1}">
<DocumentViewer.CommandBindings>
<CommandBinding Command="NavigationCommands.DecreaseZoom"
CanExecute="DecreaseZoom_CanExecute" />
</DocumentViewer.CommandBindings>
<DocumentViewer.InputBindings>
<KeyBinding Command="NavigationCommands.DecreaseZoom"
Key="OemMinus"
Modifiers="Control" />
</DocumentViewer.InputBindings>
</DocumentViewer>
Code behind -
private void DecreaseZoom_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
if (((DocumentViewer)e.Source).PageViews.Count >= 3)
{
e.CanExecute = false;
e.ContinueRouting = false;
e.Handled = true;
}
}

You can either create your custom command for this or you can create your own InputGesture, and override its behavior,
<KeyBinding.Gesture>
<CustomInputGesture/>
</KeyBinding.Gesture>

I solved my problem extending DocumentViewer and overriding method OnDecreaseZoomCommand. I tried using Custom Command but its event handler is not getting hit in case i use shortcut key "Ctrl -". But this works for me -
public class ExtendedDocumentViewer : DocumentViewer
{
protected override void OnDecreaseZoomCommand()
{
if (PageViews.Count < 3)
{
base.OnDecreaseZoomCommand();
}
}
}

Related

Silverlight Access Key Shortcuts

I need to provide access key shortcut for several functions such as Save. To do this I have started by handling the KeyUp event of my root object which is a Grid called LayoutRoot (typically created as default in a Silverlight UserControl or Page).
I am using an MVVM pattern but for this I have added code in the code behind as such (this is UI interaction so it seems OK):
private void LayoutRoot_KeyUp(object sender, KeyEventArgs e)
{
switch (e.Key)
{
case Key.S:
if ((Keyboard.Modifiers & ModifierKeys.Windows) == ModifierKeys.Windows)
{
e.Handled = true;
// save
}
break;
case Key.C:
if ((Keyboard.Modifiers & ModifierKeys.Windows) == ModifierKeys.Windows)
{
e.Handled = true;
// clear fields
}
break;
}
}
I have used the Windows key because there are no shorcuts in the browser that use it as far as I know.
I implement the Save functionality on the Save button using a Command, therefore maintaining the MVVM pattern. E.g.
public RelayCommand CommandSavePtr { get; private set; }
CommandSavePtr = new RelayCommand(OnSavePtr);
private void OnSavePtr()
{
....
In XAML: -
<Button x:Name="SavePtrButton"
Command="{Binding CommandSavePtr}"
Style="{StaticResource StandardButtonStyle}"
IsEnabled="{Binding Ptr.HasErrors, Converter={StaticResource NotOperatorValueConverter}}">
<StackPanel Orientation="Horizontal">
<Image Source="/G4SPrisonerEscorting_ResourceDictionaries;component/images/accept.png" Style="{StaticResource SubPanelIconStyle}"/>
<TextBlock Text="Save"/>
</StackPanel>
</Button>
My problem now is that I don't know how to communicate to the ViewModel from my above KeyUp event to perform the same Save function that is perfomed when clicking the Save button.
Could anyone point me in the right direction.
BTW I am using GalaSoft's MVVM Light to do the Commanding.
This is an old question, but I thought I would answer it anyway if someone else stumbles upon the same problem.
It should be possible to fire the command for the button in the code-behind in the following manner:
SavePtrButton.Command.Execute(SavePtrButton.CommandParameter);

How can I disable the default RichTextBox command for Ctrl+1?

Snoop shows that the command is "ApplySingleSpace", but when I try disabling it via the method described in this article . Like this:
<RichTextBox.CommandBindings>
<CommandBinding
Command="ApplySingleSpace"
CanExecute="BlockTheCommand"/>
</RichTextBox.CommandBindings>
.
private void BlockTheCommand(object sender,
CanExecuteRoutedEventArgs e)
{
e.CanExecute = false;
e.Handled = true;
}
My app crashes because there is no ApplySingleSpace command. ApplySingleSpace is not in the EditingCommands either.
What am I missing?
Unfortunately that will not work for
me. The reason I am trying to disable
the command is that I have a
KeyBinding in a higher nested view
that is not firing because the CTRL+1
gesture is being swallowed by the
richtextbox which has keyboardfocus.
How about overwriting that KeyBinding with a custom command that does what you want instead of trying to somehow disable it?
<RichTextBox.InputBindings>
<KeyBinding Command="local:YourCommands.Cmd1" Gesture="CTRL+1" />
<RichTextBox.InputBindings>
Taken from this question.
Using the code from this answer
How can I programmatically generate keypress events in C#?
to refire all events on PreviewKeyDown other than those you want handled by the richtextbox seems to work for me. (I only need Ctrl-C for copy). Of course you could make it so it only refires Ctrl-1 if that's what you need.
private void logKeyHandler(object sender, KeyEventArgs e)
{
if (!(Keyboard.Modifiers == ModifierKeys.Control && e.Key == Key.C))
{
e.Handled = true;
var routedEvent = Keyboard.KeyDownEvent;
this.RaiseEvent(
new KeyEventArgs(
Keyboard.PrimaryDevice,
PresentationSource.FromDependencyObject(this),
0,
e.Key) { RoutedEvent = routedEvent }
);
}
}
What about trying with the gesture instead...
<RichTextBox.InputBindings>
<KeyBinding Command="BlockTheCommand" Gesture="CTRL+1" />
</RichTextBox.InputBindings>

WPF ICommand over a button

I have implemented a custom IComand class for one of my buttons. The button is placed in a page 'MyPage.xaml' but its custom ICommand class is placed in another class, not in the MyPage code behind. Then from XAML I want to bind the button with its custom command class and then I do:
MyPage.xaml:
<Page ...>
<Page.CommandBindings>
<CommandBinding Command="RemoveAllCommand"
CanExecute="CanExecute"
Executed="Execute" />
</Page.CommandBindings>
<Page.InputBindings>
<MouseBinding Command="RemoveAllCommand" MouseAction="LeftClick" />
</Page.InputBindings>
<...>
<Button x:Name="MyButton" Command="RemoveAllCommand" .../>
<...>
</Page>
and the custom command button class:
// Here I derive from MyPage class because I want to access some objects from
// Execute method
public class RemoveAllCommand : MyPage, ICommand
{
public void Execute(Object parameter)
{
<...>
}
public bool CanExecute(Object parameter)
{
<...>
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
}
My problem is how to say MyPage.xaml that Execute and CanExecute methods for the button is in another class and not the code behind where is placed the button. How to say these methods are in RemoveAllCommand Class in XAML page.
Also I want to fire this command when click mouse event is produced in the button so I do an input binding, is it correct?
Thanks
Since you have an ICommand, you can bind it to the Button through the Command property, and the button will use it, i.e. it will call CanExecute to enable/disable itself and the Execute method when the button is pressed. There is no need for any other input binding.
Now the problem is that the button has to find the command. A simple solution is to put an instance of the command in the DataContext of the button (or of its parents).
If the DataContext has a property called RemoveAll of type RemoveAllCommand, you can simply change your XAML button to:
<Button Command="{Binding RemoveAll}" .. />
and remove the CommandBinding and InputBinding
oK, Thanks, tomorrow I'll try it.
Now to avoid problems I have moved all in RemoveAllCommandClass Class to the code behind of MyPage and I have done some modifications.
1.- I added this to MyPage.xaml:
xmlns:local="clr-namespace:GParts"
then I have done:
<Page.CommandBindings>
<CommandBinding Command="{x:Static local:Pages.MyPage._routedCommand}"
Executed="Execute"
CanExecute="CanExecute"/>
</Page.CommandBindings>
<Button Command="{x:Static local:Pages.MyPage._routedCommand}" .../>
And all is ok and works. When I press button it executes background worker (bw) that is called in Execute method. bw is into another class. In background worker class I have a variable (isRunning) that indicates if the bw is executing. Before executing DoWork event I set it to true and when bw completes, at RunWorkerCompleted, I set it to false. So from CanExecute I check isRunning in bw class and I set to true e.canExecute if isRunning is false, and e.canExecute to false if isRunning is true. So the button is disabled by WPF automatically when bw is running but when bw finishes the button continues disabled and not returns to enabled until I press it again. Why is WPF not updating the button state to enabled when bw finishes until I press again the button?
Thanks.

How to bind Close command to a button

The easiest way is to implement ButtonClick event handler and invoke Window.Close() method, but how doing this through a Command binding?
All it takes is a bit of XAML...
<Window x:Class="WCSamples.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Window.CommandBindings>
<CommandBinding Command="ApplicationCommands.Close"
Executed="CloseCommandHandler"/>
</Window.CommandBindings>
<StackPanel Name="MainStackPanel">
<Button Command="ApplicationCommands.Close"
Content="Close Window" />
</StackPanel>
</Window>
And a bit of C#...
private void CloseCommandHandler(object sender, ExecutedRoutedEventArgs e)
{
this.Close();
}
(adapted from this MSDN article)
Actually, it is possible without C# code. The key is to use interactions:
<Button Content="Close">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ei:CallMethodAction TargetObject="{Binding ElementName=window}" MethodName="Close"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
In order for this to work, just set the x:Name of your window to "window", and add these two namespaces:
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
This requires that you add the Expression Blend SDK DLL to your project, specifically Microsoft.Expression.Interactions.
In case you don't have Blend, the SDK can be downloaded here.
I think that in real world scenarios a simple click handler is probably better than over-complicated command-based systems but you can do something like that:
using RelayCommand from this article http://msdn.microsoft.com/en-us/magazine/dd419663.aspx
public class MyCommands
{
public static readonly ICommand CloseCommand =
new RelayCommand( o => ((Window)o).Close() );
}
<Button Content="Close Window"
Command="{X:Static local:MyCommands.CloseCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type Window}}}"/>
If the window was shown with Window.ShowDialog():
The simplest solution that I know of is to set the IsCancel property to true of the close Button:
<Button Content="Close" IsCancel="True" />
No bindings needed, WPF will do that for you automatically!
This properties provide an easy way of saying these are the "OK" and "Cancel" buttons on a dialog. It also binds the ESC key to the button.
Reference: MSDN Button.IsCancel property.
For .NET 4.5 SystemCommands class will do the trick (.NET 4.0 users can use WPF Shell Extension google - Microsoft.Windows.Shell or Nicholas Solution).
<Window.CommandBindings>
<CommandBinding Command="{x:Static SystemCommands.CloseWindowCommand}"
CanExecute="CloseWindow_CanExec"
Executed="CloseWindow_Exec" />
</Window.CommandBindings>
<!-- Binding Close Command to the button control -->
<Button ToolTip="Close Window" Content="Close" Command="{x:Static SystemCommands.CloseWindowCommand}"/>
In the Code Behind you can implement the handlers like this:
private void CloseWindow_CanExec(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
private void CloseWindow_Exec(object sender, ExecutedRoutedEventArgs e)
{
SystemCommands.CloseWindow(this);
}
In the beginning I was also having a bit of trouble figuring out how this works so I wanted to post a better explanation of what is actually going on.
According to my research the best way to handle things like this is using the Command Bindings. What happens is a "Message" is broadcast to everything in the program. So what you have to do is use the CommandBinding. What this essentially does is say "When you hear this Message do this".
So in the Question the User is trying to Close the Window. The first thing we need to do is setup our Functions that will be called when the SystemCommand.CloseWindowCommand is broadcast. Optionally you can assign a Function that determines if the Command should be executed. An example would be closing a Form and checking if the User has saved.
MainWindow.xaml.cs (Or other Code-Behind)
void CloseApp( object target, ExecutedRoutedEventArgs e ) {
/*** Code to check for State before Closing ***/
this.Close();
}
void CloseAppCanExecute( object sender, CanExecuteRoutedEventArgs e ) {
/*** Logic to Determine if it is safe to Close the Window ***/
e.CanExecute = true;
}
Now we need to setup the "Connection" between the SystemCommands.CloseWindowCommand and the CloseApp and CloseAppCanExecute
MainWindow.xaml (Or anything that implements CommandBindings)
<Window.CommandBindings>
<CommandBinding Command="SystemCommands.CloseWindowCommand"
Executed="CloseApp"
CanExecute="CloseAppCanExecute"/>
</Window.CommandBindings>
You can omit the CanExecute if you know that the Command should be able to always be executed Save might be a good example depending on the Application. Here is a Example:
<Window.CommandBindings>
<CommandBinding Command="SystemCommands.CloseWindowCommand"
Executed="CloseApp"/>
</Window.CommandBindings>
Finally you need to tell the UIElement to send out the CloseWindowCommand.
<Button Command="SystemCommands.CloseWindowCommand">
Its actually a very simple thing to do, just setup the link between the Command and the actual Function to Execute then tell the Control to send out the Command to the rest of your program saying "Ok everyone run your Functions for the Command CloseWindowCommand".
This is actually a very nice way of handing this because, you can reuse the Executed Function all over without having a wrapper like you would with say WinForms (using a ClickEvent and calling a function within the Event Function) like:
protected override void OnClick(EventArgs e){
/*** Function to Execute ***/
}
In WPF you attach the Function to a Command and tell the UIElement to execute the Function attached to the Command instead.
I hope this clears things up...
One option that I've found to work is to set this function up as a Behavior.
The Behavior:
public class WindowCloseBehavior : Behavior<Window>
{
public bool Close
{
get { return (bool) GetValue(CloseTriggerProperty); }
set { SetValue(CloseTriggerProperty, value); }
}
public static readonly DependencyProperty CloseTriggerProperty =
DependencyProperty.Register("Close", typeof(bool), typeof(WindowCloseBehavior),
new PropertyMetadata(false, OnCloseTriggerChanged));
private static void OnCloseTriggerChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var behavior = d as WindowCloseBehavior;
if (behavior != null)
{
behavior.OnCloseTriggerChanged();
}
}
private void OnCloseTriggerChanged()
{
// when closetrigger is true, close the window
if (this.Close)
{
this.AssociatedObject.Close();
}
}
}
On the XAML Window, you set up a reference to it and bind the Behavior's Close property to a Boolean "Close" property on your ViewModel:
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
<i:Interaction.Behaviors>
<behavior:WindowCloseBehavior Close="{Binding Close}" />
</i:Interaction.Behaviors>
So, from the View assign an ICommand to change the Close property on the ViewModel which is bound to the Behavior's Close property. When the PropertyChanged event is fired the Behavior fires the OnCloseTriggerChanged event and closes the AssociatedObject... which is the Window.

How to disable shortcuts in WPF TextBox

I want to disable all default shortcuts in WPF TextBox. Shortcuts like Ctrl + A, Ctrl + V, Ctrl + C etc. Can this be done?. It looks to me that these shortcuts are executed before KeyDown event
You can intercept the keystrokes in the PreviewKeyDown event. Set the e.Handled member to true and that will prevent the actually processing of the keys.
public Window1()
{
InitializeComponent();
CommandManager.AddPreviewCanExecuteHandler(_textBox, _canExecute);
}
private void _canExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = false;
e.Handled = true;
}
The above will prevent the TextBox from saying it can handle any command. You can selectively choose which commands you want to disable by examining the EventArgs. Or you can do this in XAML:
<TextBox x:Name="_textBox">
<TextBox.CommandBindings>
<CommandBinding Command="ApplicationCommands.Copy" CanExecute="_canExecute"/>
</TextBox.CommandBindings>
</TextBox>
Here we're just disabling the execution of the Copy command. Control-C won't work, nor will the context menu or main menu. In fact, any control that executes the Copy command will be disabled if the focus is in the TextBox.

Resources