Underscores not displayed in WPF

J4N picture J4N · Feb 25, 2011 · Viewed 13.7k times · Source

On my whole application, I've some underscores (_) which are not displayed.

It's due to the accessor. But how can I disable it? Application wide? I don't have them on labels, textboxes, ...

Thank you

Answer

Pavlo Glazkov picture Pavlo Glazkov · Feb 25, 2011

To disable underscores globally for all labels you can override the default template for labels like this:

<Style x:Key="{x:Type Label}"
       TargetType="{x:Type Label}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Label}">
                <Border Background="{TemplateBinding Background}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        Padding="{TemplateBinding Padding}"
                        SnapsToDevicePixels="true">
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                      RecognizesAccessKey="False"
                                      SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled"
                             Value="false">
                        <Setter Property="Foreground"
                                Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

It differs from the default template in this line: RecognizesAccessKey="False".

Put this style in global resources of your application (App.xaml) and your labels will not recognize underscores anymore.