Exit MenuItem Is Forever Disabled - wpf

I have a Exit menu item in a WPF application. When I moved the project over to utilize Caliburn Micro it has become disabled when launching the application.
<MenuItem Header="E_xit" InputGestureText="Alt+F4"
Command="ApplicationCommands.Close"/>
Even adding IsEnabled="True" does nothing. If I remove the command Command="ApplicationCommands.Close", the menu item is enabled on startup (but obviously I want the Close command to remain attached).
My guess is there must be some kind of method or property I'm not aware of, or maybe I am not initializing the main window properly? Here is my AppBootstrapper:
public class AppBootstrapper : BootstrapperBase
{
SimpleContainer container;
public AppBootstrapper()
{
Start();
}
protected override void Configure()
{
container = new SimpleContainer();
container.Singleton<IWindowManager, WindowManager>();
container.Singleton<IEventAggregator, EventAggregator>();
container.PerRequest<IShell, MainWindowViewModel>();
var currentParser = Parser.CreateTrigger;
Parser.CreateTrigger = (target, triggerText) => ShortcutParser.CanParse(triggerText)
? ShortcutParser.CreateTrigger(triggerText)
: currentParser(target, triggerText);
}
protected override object GetInstance(Type service, string key)
{
var instance = container.GetInstance(service, key);
if (instance != null)
return instance;
throw new InvalidOperationException("Could not locate any instances.");
}
protected override IEnumerable<object> GetAllInstances(Type service)
{
return container.GetAllInstances(service);
}
protected override void BuildUp(object instance)
{
container.BuildUp(instance);
}
protected override void OnStartup(object sender, System.Windows.StartupEventArgs e)
{
DisplayRootViewFor<IShell>();
}
protected override IEnumerable<Assembly> SelectAssemblies() {
return new[] {
Assembly.GetExecutingAssembly()
};
}
}

I suspect you haven't set CommandBinding for ApplicationCommands.Close.
In case CommandBinding could not be found, command will always return false from CanExecute method. So, to enable command you have to:
First, create CommandBinding at root element (may be at window) or at any parent element which needs to handle this command.
Second, provide CanExecute handler in that command binding and set e.CanExecute to True in that handler based on condition where you want to enable this menuItem.
Small sample to achieve what i said above:
<TextBox>
<TextBox.CommandBindings>
<CommandBinding Command="ApplicationCommands.Close"
Executed="CommandBinding_Executed"
CanExecute="CommandBinding_CanExecute"/>
</TextBox.CommandBindings>
<TextBox.ContextMenu>
<ContextMenu>
<MenuItem Header="E_xit" InputGestureText="Alt+F4"
Command="ApplicationCommands.Close"/>
</ContextMenu>
</TextBox.ContextMenu>
</TextBox>
and in code behind handlers:
private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
}
private void CommandBinding_CanExecute(object sender,
CanExecuteRoutedEventArgs e)
{
e.CanExecute = true; <-- Set this to true to enable bindings.
}
You can read more about it here - How to enable a Command and Commanding Overview.

Related

How can i trigger a event when a keyboard shortcut ( Ctrl+F7 ) is fired?

I want to fetch the event when someone in my Application uses the Shortcut Ctrl+F7. Because I have settings in my application, i dont want anyone to be able to change, but if he has to change those, I can provide him the keyboard shortcut.
How do my Application get to know, when CTRL+F7 is fired ( Application has Focus when the Shortcut is fired.)
You could override the OnPreviewKeyDown method of your main window:
protected override void OnPreviewKeyDown(KeyEventArgs e)
{
base.OnPreviewKeyDown(e);
if (e.Key == Key.F7 && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.LeftCtrl)))
{
//...
}
}
If your application has several top-level windows, you could create a common base class where you override the method and then inherit all your windows from this one:
public abstract class AppWindow : Window
{
protected override void OnPreviewKeyDown(KeyEventArgs e)
{
base.OnPreviewKeyDown(e);
if (e.Key == Key.F7 && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.LeftCtrl)))
{
//...
}
}
}
public partial class MainWindow : AppWindow
{
public MainWindow()
{
InitializeComponent();
}
}
XAML:
<local:AppWindow x:Class="WpfApp1.MainWindow">...</local:AppWindow>
You can use Command binding with Hot Key like this:
<Window.CommandBindings>
<CommandBinding Command="{x:Static local:CustomCommands.ShortCutCommand}" Executed="CommandBinding_Executed_1" CanExecute="CommandBinding_CanExecute" />
</Window.CommandBindings>
<Window.InputBindings>
<KeyBinding Command="{x:Static local:CustomCommands.ShortCutCommand}" Key="F7" Modifiers="Ctrl" />
</Window.InputBindings>
MainWindow.cs
public static RoutedCommand ShortCutCommand= new RoutedCommand();
private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
private void CommandBinding_Executed_1(object sender, ExecutedRoutedEventArgs e)
{
//do something here
}

Text changed event fired when control loaded, need to prevent on load?

In I have created a control that has a text box and a text changed event handler attached to it - this is in xaml.
The problem: when control is loaded the text changed event is fired, I do not want it to happen when the control is loaded only when I make actually make it change on the control by typing something.
What do you pros suggest I do? :)
All you have to do is check the textbox's IsLoaded property inside the event handler before handling it.
Attach Your EventHandler after the InitializeComponent Method in your constructor not in the Xaml.
i.e.
public MainWindow()
{
InitializeComponent();
textBox1.TextChanged+=new TextChangedEventHandler(textBox1_TextChanged);
}
I noticed that you are talking about an usercontrol, the only thing I can think of off the top of my head is to to create a property that can be used to inhibit the TextChanged Event until the Parent Form finishes loading. See if something like this works.
MainForm Xaml:
<my:UserControl1 setInhibit="True" HorizontalAlignment="Left" Margin="111,103,0,0" x:Name="userControl11" VerticalAlignment="Top" Height="55" Width="149" setText="Hello" />
MainForm CS
private void Window_Loaded(object sender, RoutedEventArgs e)
{
userControl11.setInhibit = false;
}
UserControl:
public UserControl1()
{
InitializeComponent();
textBox1.TextChanged += new TextChangedEventHandler(textBox1_TextChanged);
}
public string setText
{
get { return textBox1.Text; }
set { textBox1.Text = value; }
}
public bool setInhibit { get; set; }
void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
if (setInhibit) return;
// Do your work here
}
UserControl1.xaml:
<Grid>
<TextBox Text="{Binding MyText, UpdateSourceTrigger=PropertyChanged}" TextChanged="TextBox_TextChanged"/>
</Grid>
where TextChanged is the original event for TextBox
UserControl1.xaml.cs:
public partial class UserControl1 : UserControl
{
public UserControl1()
{
_isFirstTime = true;
DataContext = this;
InitializeComponent();
}
public event TextChangedEventHandler TextBoxTextChanged;
bool _isFirstTime;
//MyText Dependency Property
public string MyText
{
get { return (string)GetValue(MyTextProperty); }
set { SetValue(MyTextProperty, value); }
}
public static readonly DependencyProperty MyTextProperty =
DependencyProperty.Register("MyText", typeof(string), typeof(UserControl1), new UIPropertyMetadata(""));
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (TextBoxTextChanged != null)
if (!_isFirstTime)
{
TextBoxTextChanged(sender, e);
}
_isFirstTime = false;
}
}
where TextBox_TextChanged is the customized eventHandler for original TextChanged
and TextBoxTextChanged is more like a wrapper for the original TextChanged
Window.xaml:
<Grid>
<c:UserControl1 TextBoxTextChanged="TextBoxValueChanged"/>
</Grid>
as you see you can add an eventHandler to the event wrapper (TextBoxTextChanged)
Window.xaml.cs:
private void TextBoxValueChanged(object sender, TextChangedEventArgs e)
{
MessageBox.Show("asd");
}
finally TextBoxValueChanged won't be fired the first time Text is changed
private void TextBoxValueChanged(object sender, TextChangedEventArgs e)
{
if (Textbox1.IsFocused)
{
App.Current.Properties["TextChanged"] = "1"; // Set Flag
}
}
private void TextBoxLostFocus(object sender, RoutedEventArgs e)
{
if (App.Current.Properties["TextChanged"] == "1")
{
// Do Your Wor Here
App.Current.Properties["TextChanged"] = "0"; // Clear Flag
}
}
On your XAML:
<TextBox xName="TextBox1" LostFocus="TextBoxLostFocus" TextChanged="TextBoxValueChanged"/>
(This is a very rudimentary, dirty, codebehind hack... checking the IsLoaded property as stated by Brent I found to be efficient)
Here since on textbox control creation it's not focused, the TextChanged event will fire but the flag "1" is NOT set...
Later when user leaves field after editing it, since it had focus the Flag is set... the LostFocus is fired, but only runnig code if textbox was changed.
I found a way of preventing this behavior across multiple inputs without having to create a unique bool for each input...
private void TextChanged_UpdateItem(object sender, TextChangedEventArg e)
{
TextBox txtBox = sender as TextBox;
if (!txtBox.IsFocused)
return;
//The rest of your code here
}
So basically, if the text field doesn't have focus (like on initialization) it just returns. This also prevents it from firing if the data is changed elsewhere. :)
Alternatively, as mentioned by Brent, you can just look for "IsLoaded":
private void TextChanged_UpdateItem(object sender, TextChangedEventArg e)
{
TextBox txtBox = sender as TextBox;
if (!txtBox.IsLoaded)
return;
//The rest of your code here
}

How can I assign the 'Close on Escape-key press' behavior to all WPF windows within a project?

Is there any straightforward way of telling the whole WPF application to react to Escape key presses by attempting to close the currently focused widow? It is not a great bother to manually setup the command- and input bindings but I wonder if repeating this XAML in all windows is the most elegant approach?
<Window.CommandBindings>
<CommandBinding Command="Close" Executed="CommandBinding_Executed" />
</Window.CommandBindings>
<Window.InputBindings>
<KeyBinding Key="Escape" Command="Close" />
</Window.InputBindings>
Any constructive suggestions welcome!
All I can suggest to improve on that is to remove the need for an event handler by binding to a static command instance.
Note: this will only work in .NET 4 onwards as it requires the ability to bind to the KeyBinding properties.
First, create a command that takes a Window as a parameter and calls Close within the Execute method:
public class CloseThisWindowCommand : ICommand
{
#region ICommand Members
public bool CanExecute(object parameter)
{
//we can only close Windows
return (parameter is Window);
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
if (this.CanExecute(parameter))
{
((Window)parameter).Close();
}
}
#endregion
private CloseThisWindowCommand()
{
}
public static readonly ICommand Instance = new CloseThisWindowCommand();
}
Then you can bind your KeyBinding to the static Instance property:
<Window.InputBindings>
<KeyBinding Key="Escape" Command="{x:Static local:CloseThisWindowCommand.Instance}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" />
</Window.InputBindings>
I don't know that this is necessarily better than your approach, but it does mean marginally less boilerplate at the top of every Window and that you don't need to include an event handler in each
Or you could just add a button with Cancel as text and set IsCancel = True. Then Escape will work as default command to close.
create RoutedUICommand like below
private static RoutedUICommand EscUICommand = new RoutedUICommand("EscBtnCommand"
, "EscBtnCommand"
, typeof(WindowName)
, new InputGestureCollection(new InputGesture[]
{ new KeyGesture(Key.Escape, ModifierKeys.None, "Close") }));
and add it command binding in constructor
CommandBindings.Add(new CommandBinding(EscUICommand, (sender, e) => { this.Hide(); }));
On Windows shown with ShowDialog() you can use:
<!-- Button to close on Esc -->
<Button IsCancel="True" Width="0" Height="0"/>
You can also use PreviewKeyDown Event
PreviewKeyDown="UserControl_PreviewKeyDown"
Code behind call you close command
private void UserControl_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == Key.Escape)
{
_vm.OnCloseCommand(sender);
}
}
Another possible way is to use attached properties
Bellow is a gist code:
<script src="https://gist.github.com/meziantou/1e98d7d7aa6aa859d916.js"></script>
The Preview events happen quite early. If you have a control that should take the Esc key for its own purposes, stealing it at the window level may be too aggressive.
Instead you can handle it only if nothing else wants to:
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (!e.Handled && e.Key == Key.Escape && Keyboard.Modifiers == ModifierKeys.None)
{
this.Close();
}
}
None of above worked for me, except Kai's.
I modified his answer: I added 'btn_close.IsCancel = true;' to constructor. SettingsWindow is my second window, and main window is (default) MainWindow.
public partial class SettingsWindow : Window {
public SettingsWindow() {
InitializeComponent();
btn_close.IsCancel = true;
}
private void btn_close_Click(object sender, RoutedEventArgs e) {
this.Close();
}
}
Hope it helps,
Simon
S love nia

multi key gesture in wpf

I have a RoutedUICommand called Comment Selection. I need to add an input gesture for this command as it is in VIsual Studio, ie. (Ctrl+K, Ctrl+C).
How can I do this? Plz help me. (Keep VS functionality in mind).
Regards, Jawahar
This code is made for "Ctrl+W, Ctrl+E" and/or "Ctrl+W, E" combinations, however you can parametrize it for any key combinations:
XAML:
<MenuItem Header="Header" InputGestureText="Ctrl+W, E" Command="ShowCommand"/>
C#:
public static readonly RoutedUICommand ShowCommand = new RoutedUICommand(
"Show command text",
"Show command desc",
typeof(ThisWindow),
new InputGestureCollection(new[] { new ShowCommandGesture (Key.E) }));
public class ShowCommandGesture : InputGesture
{
private readonly Key _key;
private bool _gotFirstGesture;
private readonly InputGesture _ctrlWGesture = new KeyGesture(Key.W, ModifierKeys.Control);
public ShowCommandGesture(Key key)
{
_key = key;
}
public override bool Matches(object obj, InputEventArgs inputEventArgs)
{
KeyEventArgs keyArgs = inputEventArgs as KeyEventArgs;
if (keyArgs == null || keyArgs.IsRepeat)
return false;
if (_gotFirstGesture)
{
_gotFirstGesture = false;
if (keyArgs.Key == _key)
{
inputEventArgs.Handled = true;
}
return keyArgs.Key == _key;
}
else
{
_gotFirstGesture = _ctrlWGesture.Matches(null, inputEventArgs);
if (_gotFirstGesture)
{
inputEventArgs.Handled = true;
}
return false;
}
}
}
I've found this blog post which I think could be of help
http://kent-boogaart.com/blog/multikeygesture
Basically, WPF has no built in support for it, but subclassing InputGesture or KeyGesture seems like a possible way to achieve this without too much hassle.
Here's how I cobbled together something that actually works. I just wish I could credit the person or persons who paved the way to my Path of Enlightenment.
Let's say your application is called Heckler. Add a namespace tag for your application to the Window object:
<Window ...
xmlns:w="clr-namespace:Heckler"
...>
Now add a CommandBindings property tag and start your collection of CommandBinding objects. Here we add custom command Comment Selection:
<Window.CommandBindings>
<CommandBinding
Command="w:CustomCommands.CommentSelection"
CanExecute="CommentSelectionCanExecute"
Executed="CommentSelectionExecuted" />
</Window.CommandBindings>
Add a MenuItem to a main Menu's MenuItem:
<Menu
IsMainMenu="True">
<MenuItem
Header="_File">
<MenuItem
Command="w:CustomCommands.CommentSelection">
</MenuItem>
</MenuItem>
</Menu>
...
</Window>
In the Window code-behind, add your CustomCommands class and custom command:
public static class CustomCommands
{
// Ctrl+Shift+C to avoid collision with Ctrl+C.
public static readonly RoutedUICommand CommentSelection =
new RoutedUICommand("_Comment Selection",
"CommentSelection", typeof(MainWindow),
new InputGestureCollection()
{ new KeyGesture(Key.C, (ModifierKeys.Control | ModifierKeys.Shift)) });
}
Now wire up your event handlers:
private void CommentSelectionCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
// Determines status of command.
e.CanExecute = true;
}
private void CommentSelectionExecuted(object sender, ExecutedRoutedEventArgs e)
{
// TO-DO: Insert magic here.
}
You should be good to go. I hope this helps and I didn't miss anything!
<KeyBinding Command="{Binding ExitCommand}"
Key="{Binding ExitCommand.GestureKey}"
Modifiers="{Binding ExitCommand.GestureModifier}"/>
get
{
if (exitCommand == null)
{
exitCommand = new DelegateCommand(Exit);
exitCommand.GestureKey = Key.X;
exitCommand.GestureModifier = ModifierKeys.Control;
exitCommand.MouseGesture = MouseAction.LeftDoubleClick;
}
return exitCommand;
}
}
private void Exit()
{
Application.Current.Shutdown();
}

Interpret enter as tab WPF

I want to interpret Enter key as Tab key in whole my WPF application, that is, everywhere in my application when user press Enter I want to focus the next focusable control,except when button is focused. Is there any way to do that in application life circle? Can anyone give me an example?
Thanks a lot!
You can use my EnterKeyTraversal attached property code if you like. Add it to the top-level container on a WPF window and everything inside will treat enter as tab:
<StackPanel my:EnterKeyTraversal.IsEnabled="True">
...
</StackPanel>
Based on Richard Aguirre's answer, which is better than the selected answer for ease of use, imho, you can make this more generic by simply changing the Grid to a UIElement.
To change it in whole project you need to do this
In App.xaml.cs:
protected override void OnStartup(StartupEventArgs e)
{
EventManager.RegisterClassHandler(typeof(UIElement), UIElement.PreviewKeyDownEvent, new KeyEventHandler(Grid_PreviewKeyDown));
base.OnStartup(e);
}
private void Grid_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
var uie = e.OriginalSource as UIElement;
if (e.Key == Key.Enter)
{
e.Handled = true;
uie.MoveFocus(
new TraversalRequest(
FocusNavigationDirection.Next));
}
}
Compile.
And done it. Now you can use enter like tab.
Note: This work for elements in the grid
I got around woodyiii's issue by adding a FrameworkElement.Tag (whose value is IgnoreEnterKeyTraversal) to certain elements (buttons, comboboxes, or anything I want to ignore the enter key traversal) in my XAML. I then looked for this tag & value in the attached property. Like so:
if (e.Key == Key.Enter)
{
if (ue.Tag != null && ue.Tag.ToString() == "IgnoreEnterKeyTraversal")
{
//ignore
}
else
{
e.Handled = true;
ue.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
}
}
woodyiii, There is a function in the UIElement called PredictFocus() which by its name know its function, then you can check if that element is enabled or not so as to move the focus to it or not...
Here is Matt Hamilton's code, if anyone is wondering since his site is down apparently:
public class EnterKeyTraversal
{
public static bool GetIsEnabled(DependencyObject obj)
{
return (bool)obj.GetValue(IsEnabledProperty);
}
public static void SetIsEnabled(DependencyObject obj, bool value)
{
obj.SetValue(IsEnabledProperty, value);
}
static void ue_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
var ue = e.OriginalSource as FrameworkElement;
if (e.Key == Key.Enter)
{
e.Handled = true;
ue.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
}
}
private static void ue_Unloaded(object sender, RoutedEventArgs e)
{
var ue = sender as FrameworkElement;
if (ue == null) return;
ue.Unloaded -= ue_Unloaded;
ue.PreviewKeyDown -= ue_PreviewKeyDown;
}
public static readonly DependencyProperty IsEnabledProperty =
DependencyProperty.RegisterAttached("IsEnabled", typeof(bool),
typeof(EnterKeyTraversal), new UIPropertyMetadata(false, IsEnabledChanged));
static void IsEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var ue = d as FrameworkElement;
if (ue == null) return;
if ((bool)e.NewValue)
{
ue.Unloaded += ue_Unloaded;
ue.PreviewKeyDown += ue_PreviewKeyDown;
}
else
{
ue.PreviewKeyDown -= ue_PreviewKeyDown;
}
}
}
Another, a more on/off implementation approach would be to use behaviors:
public class TextBoxEnterFocusesNextBehavior :
Behavior<TextBox>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.PreviewKeyDown += AssociatedObjectOnPreviewKeyDown;
}
protected override void OnDetaching()
{
AssociatedObject.PreviewKeyDown -= AssociatedObjectOnPreviewKeyDown;
base.OnDetaching();
}
private void AssociatedObjectOnPreviewKeyDown(object sender, KeyEventArgs args)
{
if (args.Key != Key.Enter) { return; }
args.Handled = true;
AssociatedObject.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
}
}
Usage example:
<UserControl xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
xmlns:behaviors="clr-namespace:Your.Namespace.To.Behaviors"
...>
<DockPanel>
<TextBox x:Name="TextBoxWithBehavior"
DockPanel.Dock="Top">
<b:Interaction.Behaviors>
<behaviors:TextBoxEnterFocusesNextBehavior />
</b:Interaction.Behaviors>
</TextBox>
<TextBox x:Name="TextBoxWithoutBehavior"
DockPanel.Dock="Top" />
<TextBox x:Name="AnotherTextBoxWithBehavior"
DockPanel.Dock="Top">
<b:Interaction.Behaviors>
<behaviors:TextBoxEnterFocusesNextBehavior />
</b:Interaction.Behaviors>
</TextBox>
</DockPanel>
</UserControl>
My solution:
public class MoveToNext : TriggerAction<DependencyObject>
{
protected override void Invoke(object parameter)
{
if (parameter is RoutedEventArgs routedEventArgs && routedEventArgs.OriginalSource is FrameworkElement element)
{
routedEventArgs.Handled = true;
element.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
}
}
}
Usage:
<StackPanel>
<i:Interaction.Triggers>
<i:KeyTrigger Key="Return">
<util:MoveToNext/>
</i:KeyTrigger>
</i:Interaction.Triggers>
<!-- put your controls here -->
</StackPanel>
If you want the behavior to be attached to only one control instead of all controls within a layouter, simply add the <i:Interaction.Triggers block to that specific control.

Resources