Mafii Mafii - 3 months ago 11
C# Question

Allow WPF button navigation via keys with stringformat and _

Lets say I have a button:

<Button Name="BtnOk"
Content="OK"
IsDefault="True">


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"
Content="_OK">


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"
    ContentStringFormat="_{0}">



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}"

Answer

Have you tried to use AccessText:

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

UPDATE

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}}" />
</Button>
Comments