0

I've overridden my Button style in my application, which seemingly removed the ability for me to use Button.Content with an _ to assign an access key, therefore leaving me with the only option of use AccessText. I viewed a few questions which dealt with how to make a Button have an access key again, such as How to use an accesskey on a WPF Button with a custom ContentTemplate?, which solved my issue up until now.

I have a Window where the Button's Content was set programmatically, depending on how you got to that Window.

This is how I was assigning an access key before:

<Button x:Name="Button1" Text="_Hello" />

and it worked just fine, properly displaying an underlined H in Hello. Since changing the style, it no longer works that way.

This is how AccessText is normally done:

<Button x:Name="Button1">
    <AccessText>_Hello</AccessText>
</Button>

which shows up with the H in Hello underlined, regardless of custom styles.

Unfortunately, the only Property I can find which allows you to modify what a button displays is Content, and when I try to assign Button.Content like so:

Button1.Content = "_Goodbye";

it displays _Goodbye instead of treating it like an access key like it did before I applied my custom style. The G in Goodbye is not underlined, and unable to be used as an access key.

Therefore, my question is: Is it possible to assign AccessText to a WPF Button programmatically after modifying the Button's style?

pfthroaway
  • 85
  • 1
  • 8

1 Answers1

2

You can set the content of the Button with an AccessText object and set its Text property.

Button1.Content = new AccessText
{
    Text = "_Goodbye",
};
Lockdowne
  • 474
  • 3
  • 7