Mafii Mafii - 7 months ago 28
C# Question

Allow WPF button navigation via keys with stringformat and _

Lets say I have a button:

<Button Name="BtnOk"

Now when I place a _ infront of the content, I can use the button with the key that follows after, e.g. I can use the following button directly (without focus onto him) with pressing the key o.

<Button Name="BtnOk"

How would I do that with a binding?

I've tried the following two approaches:

  1. Doesn't display a
    at all:

    <Button Name="BtnOk"
    Content="{Binding SomePath, StringFormat=_{0}">

  2. Displays the underscore before the letter, instead of underneath him:

    <Button Name="BtnOk"
    Content="{Binding SomePath"

How do I get the underscore underneath the first letter of the word so navigation can be done with a keystroke of that letter?

The button is in a usercontrol that I use from somewhere else, and the text of the button is binded to a dependencyproperty like this:

Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MyUserControl}}, Path=SomePath}"


Have you tried to use AccessText:

<Button Name="BtnOk" Content="OK"IsDefault="True">
    <AccessText Text={Binding SomePath} />


As your binding doesn't have an underscore you can use StringFormat to tackle with.

<Button Name="BtnOk" IsDefault="True">
    <AccessText Text="{Binding SomePath, StringFormat=_{0}}" />