HelloWorld
HelloWorld

Reputation: 1091

Label doesn't display "_" character

My Label.Content in WPF doesn't display the first occurrence of "_" character. Why?

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="148" Width="211">
    <Grid>
        <Label Content="L_abel" Height="28" HorizontalAlignment="Left" Margin="37,31,0,0" Name="label1" VerticalAlignment="Top" />
    </Grid>
</Window>

enter image description here

When set Label.Content ="L__abel" :

enter image description here

There is no additional code in project.

Upvotes: 50

Views: 33322

Answers (8)

Paul Wichtendahl
Paul Wichtendahl

Reputation: 127

Reading through the documentation and trying several of the solutions here I borrowed from the @BrightShadow and came up with this solution. What I liked about it is it turned off the hot key/accessor only for the label where I wanted it to. This is excerpted from a project that displays properties of scripts and SQL queries.

<Label x:Name="lblParmName" Grid.Column="0" Grid.Row="0" FontWeight="Bold" 
               HorizontalAlignment="Right" Margin="0,0,2,0" Content="@End_Date">
       <Label.Template>
             <ControlTemplate TargetType="Label">
                  <ContentPresenter RecognizesAccessKey="False" 
                                    HorizontalAlignment="Right" 
                                    VerticalAlignment="Center"/>
              </ControlTemplate>
       </Label.Template>
</Label>

I could have gone farther and bound the alignment properties to the parent but I wanted to show the easy way to retain the properties of the Label control and still be able to show text with an "underscore". Hope this helps someone.

Upvotes: 0

B Duffy
B Duffy

Reputation: 21

I have several places where text bound into a Label control has to display the '_' character. So I wrote a converter:

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace Converters
{
    public class TextToLabelConverter : DependencyObject, IValueConverter
    {
        /// <inheritdoc />
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (value is string strValue)
                ? strValue.Replace("_", "__")
                : Binding.DoNothing;
        }

        /// <inheritdoc />
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

Use it in the XAML thusly:

...
    xmlns:converters="clr-namespace:Converters"
...

<Window.Resources>
    <converters:TextToLabelConverter x:Key="TextToLabelConverter" />
</Window.Resources>

<Grid>
    <Label Content="{Binding SourceText, Converter={StaticResource TextToLabelConverter}}" />
</Grid>

Upvotes: 2

Jason McClinsey
Jason McClinsey

Reputation: 446

Use of TextBlock to solve this issue comes with some disadvantages, such as the inability to center the content vertically (save for placing it within a grid, and there are many situations where the requisite additional controls may be undesirable). In my case, I created a TextBox that behaves like a Label using the following code:

var fauxLabel = new TextBox();
fauxLabel.Cursor = Cursors.Arrow; // Avoid the IBeam mouse cursor when hovering
fauxLabel.Background = Brushes.Transparent;
fauxLabel.BorderThickness = new Thickness(0.0, 0.0, 0.0, 0.0);
fauxLabel.Focusable = false;

Upvotes: 0

Onkelbummms
Onkelbummms

Reputation: 226

The easiest method to fix it would be:

Change

<Label Content="L_abel" Height="28" HorizontalAlignment="Left" Margin="37,31,0,0" Name="label1" VerticalAlignment="Top" />

to

<Label Height="28" HorizontalAlignment="Left" Margin="37,31,0,0" Name="label1" VerticalAlignment="Top">
<TextBlock Text="L_abel"/>
</Label>

Upvotes: 14

BrightShadow
BrightShadow

Reputation: 463

In WPF there is an attribute called RecognizesAccessKey, try to change it to false. If you use RadioButton be aware that there is also label behind, and in the RadioButton template to disable access key recognition you must set RecognizesAccessKey="False" on template ContentPresenter. Then this is disabled, or label is replaced with something else I don't remember now.

Upvotes: 9

Knasterbax
Knasterbax

Reputation: 8207

Joey is right! Use

<TextBlock>L_abel</TextBlock>

and all your underscores will be displayed!

Upvotes: 11

Ignacio Soler Garcia
Ignacio Soler Garcia

Reputation: 21855

Because the _ letter is used for shortcuts (is an accelerator)

Upvotes: 4

Joey
Joey

Reputation: 354356

_ is used in WPF to signal an access key, i.e. a key you can press with Alt to give focus or invoke an UI element. This is similar to how & is used in the Windows API and Windows Forms. Since labels are intended to be used as the label for another control (to describe a text box, for example), this is pretty much expected. You should see the a in your example underlined when you press Alt.

From the documentation:

To set the access key, add an underscore before the character that should be the access key. If your content has multiple underscore characters, only the first one is converted into an access key; the other underscores appear as normal text. If the underscore that you want converted to the access key is not the first underscore, use two consecutive underscores for any underscores that precede the one that you want to convert. For example, the following code contains an access key and displays as _HelloWorld:

<Label>__Hello_World</Label> 

Because the underscore that precedes H is a double, the W key registers as the access key.

I guess if you neither require nor want the features Label provides, you may use a TextBlock.

Upvotes: 63

Related Questions