.net 标签不显示“_”字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9684619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Label doesn't display "_" character
提问by HelloWorld
My Label.Contentin WPF doesn't display the first occurrence of "_" character. Why?
我Label.Content在 WPF 中不显示“_”字符的第一次出现。为什么?
<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>


When set Label.Content ="L__abel":
设置时Label.Content ="L__abel":


There is no additional code in project.
项目中没有额外的代码。
回答by Joey
_is used in WPF to signal an access key, i.e. a key you can press with Altto 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 ain your example underlined when you press Alt.
_在 WPF 中用于表示访问键,即您可以按下Alt以提供焦点或调用 UI 元素的键。这类似于如何&在 Windows API 和 Windows 窗体中使用。由于标签旨在用作另一个控件的标签(例如,用于描述文本框),因此这在意料之中。a当您按下 时,您应该会在示例中看到带下划线的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.
要设置访问键,请在应该作为访问键的字符前添加下划线。如果您的内容有多个下划线字符,则只有第一个被转换为访问密钥;其他下划线显示为普通文本。如果要转换为访问键的下划线不是第一个下划线,请对要转换的下划线之前的任何下划线使用两个连续的下划线。例如,以下代码包含访问密钥并显示为 _HelloWorld:
<Label>__Hello_World</Label>因为 H 前面的下划线是双数,所以 W 键注册为访问键。
I guess if you neither require nor want the features Labelprovides, you may use a TextBlock.
我想如果您既不需要也不想要Label提供的功能,您可以使用TextBlock.
回答by Knasterbax
Joey is right! Use
乔伊是对的!用
<TextBlock>L_abel</TextBlock>
and all your underscores will be displayed!
并且您所有的下划线都将显示出来!
回答by BrightShadow
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.
在 WPF 中有一个名为RecognizesAccessKey的属性,尝试将其更改为 false。如果您使用 RadioButton 请注意后面还有标签,并且在 RadioButton 模板中要禁用访问键识别,您必须RecognizesAccessKey="False"在模板上设置ContentPresenter。然后这是禁用的,或者标签被替换为我现在不记得的其他东西。
回答by Onkelbummms
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>
回答by Ignacio Soler Garcia
Because the _ letter is used for shortcuts (is an accelerator)
因为_字母是用来做快捷方式的(是一个加速器)
回答by Jason McClinsey
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:
使用 TextBlock 来解决这个问题有一些缺点,例如无法垂直居中内容(除了将其放置在网格中,并且在许多情况下可能不需要必要的附加控件)。就我而言,我使用以下代码创建了一个行为类似于 Label 的 TextBox:
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;

