2

I have a list of menu items that are bound to a string collection. Each of these menu items, when clicked, will fire my Open command. This is all working fine.

How do I omit the Ctrl+O next to each of these menu items?

Below is my XAML.

                <MenuItem Header="Recent packages" ItemsSource="{Binding Source={x:Static p:Settings.Default}, Path=RecentItems}">
                <MenuItem.ItemTemplate>
                    <DataTemplate>
                        <MenuItem CommandTarget="{Binding}" Command="ApplicationCommands.Open" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Header}">
                        <MenuItem.Header>
                                <TextBlock>
                                    <TextBlock.Text>
                                        <Binding StringFormat="{}{0}" />
                                    </TextBlock.Text>
                                </TextBlock>
                            </MenuItem.Header>
                        </MenuItem>
                    </DataTemplate>
                </MenuItem.ItemTemplate>
            </MenuItem>

Here is a picture of what I am talking about. enter image description here

Dbloom
  • 1,302
  • 3
  • 18
  • 45
  • do a google search on the follow, that would have been the first thing I would have done `C# stackoverflow TextBlock.Text Binding StringFormat` – MethodMan Apr 26 '17 at 21:31
  • Possible duplicate of [Hide or disable Input Gesture Text in wpf](http://stackoverflow.com/questions/3616555/hide-or-disable-input-gesture-text-in-wpf) – Samuel Jenks Apr 26 '17 at 21:53

1 Answers1

1

How do I use StringFormat to get rid of the InputGesture text when a UI element is bound to a command?

You don't. The Header property has nothing to do with the display of the gesture text, and applying StringFormat or anything else to it won't have any effect.

You could use the approach found in Hide or disable Input Gesture Text in wpf, suggested by the comment above. In that case, you'd probably just set the alpha to 0 for the text color. But a simpler approach would be to just set the MenuItem.InputGestureText value to " " (note the space…if you use the empty string, WPF will ignore it).

All that said, I think you are approaching this the wrong way.

The reason you are getting the gesture text in the first place is because you are using ApplicationCommands.Open as the command for the menu item. But that's not really the right command. You already use that, for the "Open package…" command in your "File" menu. So it's not really the same command, is it?

Even if you hide the input gesture text, that doesn't actually remove the gesture. It just doesn't document it in the menu. So, when the user presses Ctrl+O, which of these three menu items is it you expect to be executed? How do you expect WPF to be sure it will execute the command you wanted?

In my opinion, you should define your own ICommand for use with these menu items, and leave the ApplicationsCommand.Open command out of it. Reserve that for the one command you do want a keyboard shortcut for. With your own ICommand (e.g. a RoutedUICommand) you can simply not define an input gesture/keyboard shortcut, and then there won't be any to display in the menu.

Community
  • 1
  • 1
Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
  • I am trying to re-use the same Open command to avoid having more code that does mostly the same thing: Open a file. The difference between the code run when a user clicks File->Open and when they click Recent packages->FileName is that the former opens a dialog box to let the user choose the file to open, and the latter uses the path from the menu to open the file. – Dbloom Apr 27 '17 at 17:44
  • But I do agree with the points you bring up. Perhaps I could let the Open command and this new RoutedUICommand handle the task of getting the file to open, and then use a common method to actually do the opening, thus avoiding repeating code in two difference places. – Dbloom Apr 27 '17 at 17:44