Reputation: 20697
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
Upvotes: 26
Views: 17579
Reputation: 1038
I ran into the same problem for Button
& Label
Control where the underscore inside the content was not displayed. I also want the fix without changing the look and feel of the Button or Label. I got the idea from this question on Stackoverflow. I tried using doubling the underscores
, but it had its issues when you tried to access the content of the label or button you now have to convert double underscore to single.
I implemented the solution using DataTemplate applied through style to ContentControl. I set RecognizesAccessKey="False"
for the ContentPresenter.As both Label
& Button
control derive from ContentControl
with Content Property. So We have to apply the style to them using the BasedOn
attribute as shown in the code.
Here is how the UI looks before and after the fix:
Here is the code for MainWindow.xaml to test:
<Window x:Class="ContentControlWithUnderscore.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" SizeToContent="Height" Width="300">
<StackPanel>
<TextBlock Background="LightBlue" >Original Text:</TextBlock>
<StackPanel>
<TextBlock Text="Some_Text"></TextBlock>
<TextBlock Text="Some__Text"></TextBlock>
<TextBlock Text="Som_e_Text"></TextBlock>
</StackPanel>
<TextBlock Background="LightBlue">Original Problem:</TextBlock>
<StackPanel >
<Button Content="Some_Text"></Button>
<Button Content="Some__Text"></Button>
<Button Content="Som_e_Text"></Button>
<Label Content="Some_Text"></Label>
<Label Content="Some__Text"></Label>
<Label Content="Som_e_Text"></Label>
</StackPanel>
<TextBlock Background="LightBlue">After Fix:</TextBlock>
<StackPanel >
<StackPanel.Resources>
<Style TargetType="ContentControl">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<ContentPresenter RecognizesAccessKey="False" Content="{Binding Path=Content, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentControl}}}"></ContentPresenter>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- Inherit the ContentControl style for Button -->
<Style TargetType="Button" BasedOn="{StaticResource {x:Type ContentControl}}"></Style>
<!-- Inherit the ContentControl style for Label -->
<Style TargetType="Label" BasedOn="{StaticResource {x:Type ContentControl}}"></Style>
</StackPanel.Resources>
<Button Content="Some_Text"></Button>
<Button Content="Some__Text"></Button>
<Button Content="Som_e_Text"></Button>
<Label Content="Some_Text"></Label>
<Label Content="Some__Text"></Label>
<Label Content="Som_e_Text"></Label>
</StackPanel>
</StackPanel>
</Window>
Upvotes: 1
Reputation: 11680
One easy solution is to not use <Label>. <TextBox> doesn't mess with underscores.
Upvotes: 11
Reputation: 20746
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.
Upvotes: 25