0

I'm working on a WPF application, one of the requirement is to display the shortcut key hint on the button when Alt key is pressed. It needs to work same way as ribbon/menu does when you press Alt key, it shows hints. I'm using MVVM and it needs to be implemented at multiple places not just one button so needs a generic solution (maybe behavior or something).

Is there a way to implement this? Any help would be appreciated.

H.B.
  • 166,899
  • 29
  • 327
  • 400
azeemfraz
  • 1
  • 1
  • I'm pretty sure you get this out-of-the-box, no? – lc. Jul 17 '14 at 16:19
  • No, you can set accesstext or with _ButtonText but nothing like ribbon where you see a hint at the corner. – azeemfraz Jul 17 '14 at 16:30
  • Fair enough, I stand corrected then. – lc. Jul 17 '14 at 16:35
  • 1
    Yes there is a way to implement this. Create a new button type that includes the hint in the template and listens for the alt key being pressed. Come back when you've started that and are stuck. –  Jul 17 '14 at 19:54
  • You can also write an Adorner used via a behavior. In this way you can attach the behavior to any button. – nano Jul 17 '14 at 20:03
  • For other's reference, I got similar answer on different website with some code to start with (thanks to Andy) http://social.msdn.microsoft.com/Forums/en-US/3f747874-ee35-43a1-b2ca-4f5a19b92309/show-shortcut-key-hint-on-a-button-just-like-ribbon-when-alt-key-is-pressed?forum=wpf – azeemfraz Jul 17 '14 at 21:42

1 Answers1

0

Don’t know what is your container element ribbon or menu(should work on any) so for example lets say its simple grid. Here’s the basic guideline:

1) In your ViewModel create a prop of bool type called HintVisible.

2) Your containing element (grid) will contain hint elements (for example buttons) that will be visible when hint needed. Their Visibility prop should be bound to HintVisible prop of VM

<Button Visibility=”{Binding HintVisible , Converter={StaticResource BoolToVisibilityConverter} }”>Hint</Button>

3) Your containing element should have

<Grid>
<Grid.InputBindings>
    <KeyBinding Command="{Binding ToggleHintCommand}" Key="LeftAlt"/>
</Grid.InputBindings>
<!-- Elements-->
</Grid>

4) The ToggleHintCommand of your VM should toggle the HintVisible prop

Den
  • 360
  • 1
  • 9
  • I think you didn't understand the question. What you are suggesting is a way to show/hide hint but how would you draw the hint on a button like ribbon does when Alt key is pressed? – azeemfraz Jul 21 '14 at 19:10
  • And what is a complication? The most straighforwrd way - your ribbon is in a grid, you'll just have to put your buttons under ribbon (in visual tree) and use for example margin prop to position them over a right tab of ribbon. You can always use bindings (to flexibly adapt for changing of position and resizing) for more complicated behavior – Den Jul 22 '14 at 03:57
  • More advanced way - I never worked with ribbon, but its wpf... so you should be able to DataTemplate its tab header, so just put the hint there using a methode I mentioned earlier – Den Jul 22 '14 at 04:05