Search code examples
wpfxamlbuttonunderline

How can I underline a single character in the Content of a Button in WPF?


I have a number of buttons in my WPF window and I would like to have certain characters in the button contents underlined.

I have tried using the "_" like "My_Content" to underline the C, however this does not appear until the user hits the Alt key, or has their local settings changed. Using < Underline > within the Content property causes an error when I attempt to underline only part of the content like:

Content="My< Underline >C< /Underline >ontent".

I would prefer to set this in the XAML if possible. Any help would be appreciated.

Thank You!


Solution

  • You would have to do this explicitly like so:

    <Button>
        <Button.Content>
            <TextBlock>
                My <Underline>C</Underline>ontent
            </TextBlock>
        </Button.Content>
    </Button>
    

    This would remove the ability to click the button using Alt+Char though. For that an AccessText element is used. But that doesn't support the markup syntax of TextBlock.