Search code examples
c#wpfbuttonaccessibility

how to set accelerator (access key) for an imagebutton


Imagebutton is a hybrid consisting of image and text. See here:

I use the first method because it is most straightforward for me.

Now, the problem is I would like to have an accelerator for such button which would behave exactly like accelerator for a normal button.

I tried to register access key and use textblock for displaying text, but then I am unable to show underscore. When I use label instead of textblock then I get underscore, but label treats it as access key on its own overriding registered access key for button -- so when I press it the label is just focused, but there is no "click" action for button.

How to have both worlds -- underscore when [alt] is pressed and "click" action for button?

Thank you in advance for help.


Solution

  • It sounds like you need to set the Target property for the Label.

    For example:

    <Button x:Name="myButton">
        <StackPanel>
            <Image/>
            <Label Target="{Binding ElementName=myButton}">
                _Click Me
            </Label>
        </StackPanel>
    </Button>