在我的整个应用程序中,我有一些未显示的下划线(_)。
这是由于访问器造成的。但我怎样才能禁用它呢?应用广泛?我的标签、文本框上没有它们......
谢谢你
要全局禁用所有标签的下划线,您可以覆盖标签的默认模板,如下所示:
<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>
它与此行中的默认模板不同:
RecognizesAccessKey="False"
。
将此样式放入应用程序的全局资源中(
App.xaml
),您的标签将不再识别下划线。
使用两个下划线:
name = "__something";
一个简单的解决方案是不使用
您尝试过加倍下划线吗?
我遇到了同样的问题
Button
控制内容中的下划线不显示。我也不想修改 Button 的控件模板来解决这个问题。我喜欢doubling the underscores
的解决方案。我使用通过样式应用的 AttachedProperty
来实现该解决方案。
这是要测试的XAML代码:
<Window x:Class="GridCtrl.ButtonWithUnderScore"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:GridCtrl"
mc:Ignorable="d"
Title="MainWindow" Height="200" Width="300">
<StackPanel>
<TextBlock>Original Problem:</TextBlock>
<StackPanel>
<Button Content="Some_Text"></Button>
<Button Content="Some__Text"></Button>
<Button Content="Som_e_Text"></Button>
</StackPanel>
<TextBlock>After Fix:</TextBlock>
<StackPanel>
<StackPanel.Resources>
<Style TargetType="Button">
<Setter Property="local:ButtonExtensions.ReplaceUnderscore" Value="True"/>
</Style>
</StackPanel.Resources>
<Button Content="Some_Text"></Button>
<Button Content="Some__Text"></Button>
<Button Content="Som_e_Text"></Button>
</StackPanel>
</StackPanel>
</Window>
这是按钮的附加属性:
public static class ButtonExtensions
{
public static readonly DependencyProperty ReplaceUnderscoreProperty =
DependencyProperty.RegisterAttached(
"ReplaceUnderscore",
typeof(bool),
typeof(ButtonExtensions),
new PropertyMetadata(false, OnReplaceUnderscoreChanged));
public static void SetReplaceUnderscore(DependencyObject element, bool value)
{
element.SetValue(ReplaceUnderscoreProperty, value);
}
public static bool GetReplaceUnderscore(DependencyObject element)
{
return (bool)element.GetValue(ReplaceUnderscoreProperty);
}
private static void OnReplaceUnderscoreChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is Button button)
{
if ((bool)e.NewValue)
{
button.Loaded += (sender, args) =>
{
if (button.Content is string content)
{
button.Content = content.Replace("_", "__");
}
};
}
}
}
}