Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
748 views
in Technique[技术] by (71.8m points)

.net - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

_ 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...