71181

My key shortcut isn't working

Question:

I am tryiong to add a shortcut to my menu Item but It doesn't seams to work.

here is my menu item:

<MenuItem Name="openMenuItem" Header="Open file" InputGestureText="Ctrl+O" Command="{Binding OpenFile}"></MenuItem>

What am I doing wrong here?

here is a picture of my menu:

<img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/JFnXk.png" data-original="https://i.stack.imgur.com/JFnXk.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

Answer1:

In response to the question you specifically asked:

<blockquote>

What am I doing wrong here?

</blockquote>

I direct you to the <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.menuitem.inputgesturetext.aspx" rel="nofollow">documentation for the InputGestureText property</a>:

<blockquote>

This property does not associate the input gesture with the menu item; it simply adds text to the menu item.

</blockquote>

It's behaving as designed. InputGestureText just displays some text on the menu item; it does not change behavior, and in particular, it does not actually listen for that key gesture. This is somewhat unexpected, given that the corresponding property in WinForms <em>does</em> add behavior, but it's also called out by the Text suffix on the property name -- it's not the input gesture, it's just the text that's displayed to <em>tell</em> the user about the input gesture. It's set automatically when you use RoutedUICommand, but when you implement ICommand yourself, it's up to you to both set InputGestureText and listen for the key gesture.

What you're doing wrong is expecting this property to behave intuitively. You're far from the only one to be confused by this.

(The obvious follow-on question is "how do I add a keyboard shortcut for my MVVM command", but that's a separate question -- and one that's been asked and answered on StackOverflow multiple times; once you know that's the right question to be asking, you should be able to search for existing answers.)

Answer2:

According to Nick at: <a href="http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/d5536d93-b570-4e21-8668-641fa519cd95/" rel="nofollow">http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/d5536d93-b570-4e21-8668-641fa519cd95/</a> you need to add code to make it respond to the shortcut, like this:

public Window1() { FilterCommand.InputGestures.Add(new KeyGesture(Key.O, ModifierKeys.Control)); InitializeComponent(); }

Answer3:

There are predefined commands for Open and Close and other common ones. Take a look at : <a href="http://msdn.microsoft.com/en-us/library/system.windows.input.applicationcommands.aspx" rel="nofollow">ApplicationCommands</a>

You would have something like :

<Menu DockPanel.Dock="Top"> <MenuItem Command="ApplicationCommands.Paste" Width="75" /> </Menu>

Recommend

  • C++ multiple threads and vectors
  • Silverlight ChildWindow does not reposition correctly
  • WCF message with xs:anytype causing serialization problems
  • NSPredicate acting strange in NSFetchedResultsController
  • using 64bit and 32 bit JDK
  • Collecting attributes from dask dataframe providers
  • Get URL Query String Parameters when there are multiple parameters with the same name using Jquery o
  • How to associate .mp3 files in Android Studio
  • Is it safe to accept URL parameters for populating the `url_for` method?
  • How many Vertica Databases can run on a Host in the same time?
  • How do you create a Fuseki SPARQL server using the Apache Jena Java API?
  • Microsoft Excel Pivot miscalculation in Sum for positive and negative numbers
  • What is the use of a session store?
  • CSS - Cannot get one spanned style to override another inherited style and align left
  • Compare struct to a constant in C
  • Wrong labels when plotting a time series pandas dataframe with matplotlib
  • Suppressing passwd when calling sqlplus from shell script
  • Ember.js model to be organised as a tree structure
  • Android application: how to use the camera and grab the image bytes?
  • How does document.ready work with angular element directives?
  • How do I get HTML corresponding to current DOM tree?
  • JQuery Internet Explorer and ajaxstop
  • JSON response opens as a file, but I can't access it with JavaScript
  • Database structure design with variable amounts of fields
  • Paperclip, set path outside of rails root folder
  • QLineEdit password safety
  • Why is the size of this struct 32?
  • How do I fake an specific browser client when using Java's Net library?
  • How to get a value (ex: baseURL) in every Karate feature?
  • MySQL WHERE-condition in procedure ignored
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • Change an a tag attribute in JavaScript based on screen width
  • Web-crawler for facebook in python
  • Calling of Constructors in a Java
  • Traverse Array and Display in markup
  • Transpose CSV data with awk (pivot transformation)
  • trying to dynamically update Highchart column chart but series undefined
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • Checking variable from a different class in C#
  • java string with new operator and a literal