我正在将一个标签的.Content
值设置为一个包含下划线的字符串;第一个下划线被解释为加速键。
在不更改基础字符串(通过替换所有_
为__
)的情况下,有没有一种方法可以禁用标签的加速器?
我正在将一个标签的.Content
值设置为一个包含下划线的字符串;第一个下划线被解释为加速键。
在不更改基础字符串(通过替换所有_
为__
)的情况下,有没有一种方法可以禁用标签的加速器?
如果您将TextBlock用作Label的内容,它的文本不会吸收下划线。
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Grid.Resources>
<Style x:Key="{x:Type Label}" BasedOn="{StaticResource {x:Type Label}}" TargetType="Label">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Label">
<Border>
<ContentPresenter
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
RecognizesAccessKey="False" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<Label>_This is a test</Label>
</Grid>
</Page>
WinForms
不同,其中所有内容都在代码后面。 - XAMlMAX<Border Padding="{TemplateBinding Padding}">
。如果没有这个,填充就不会被合并,与默认模板相比,这个模板的 Label
的视觉外观是不同的。 - Jonathan Gilbert<TextBlock> ... </TextBlock>
代替 <Label> ... </Label>
来打印带下划线的确切文本。public partial class LabelEx : Label
{
public bool PreventAccessKey { get; set; } = true;
public LabelEx()
{
InitializeComponent();
}
public new object Content
{
get
{
var content = base.Content;
if (content == null || !(content is string))
return content;
return PreventAccessKey ?
(content as string).Replace("__", "_") : content;
}
set
{
if (value == null || !(value is string))
{
base.Content = value;
return;
}
base.Content = PreventAccessKey ?
(value as string).Replace("_", "__") : value;
}
}
}
Label
不仅处理加速器,还适用于其他控件(例如GroupBox
),这些控件不能被TextBlock
替代。 - GraemeF