Setting InputGestureText on a MenuItem bound to a Command in WPF

If you want to set the InputGestureText property of a MenuItem that is bound to a Command in WPF, make sure you set it before the Command property is set, for example this is OK (notice the elegant way to set it to an empty string ;-)):

<ContextMenu>
    <MenuItem InputGestureText="{x:Static sys:String.Empty}" Command="…" CommandTarget="…"/>
</ContextMenu>

If you are wondering, you would want to set InputGestureText to an empty string in case the context menu (or menu item) is not available from keyboard at all, i.e. the placement target is not focusable.

This is not OK, i.e. the command default input gesture text will replace your text in this case:

<ContextMenu>
    <MenuItem Command="…" CommandTarget="…" InputGestureText="…"/>
</ContextMenu>

About Sorin Dolha

My passion is software development, but I also like physics.
This entry was posted in Computers and Internet. Bookmark the permalink.

2 Responses to Setting InputGestureText on a MenuItem bound to a Command in WPF

  1. Pingback: 2010 in review | Sorin Dolha's Blog

  2. Tedy says:

    The order doesn’t matter if you don’t set the InputGestureText as empty string. You can give it a space which is also invisible on UI.

Add a reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s