在 WPF 内容中看不到 _(下划线)

发布于 2024-12-11 18:49:25 字数 192 浏览 0 评论 0原文

一个非常简单的问题:

为什么我在WPF内容中看不到_(下划线)?

例如 的内容

<Label Content="test_t" Name="label2"  />

显示为 "testt" (不显示下划线)。

A very simple question:

Why I cant see _ (underscore) in WPF content?

For instance the content of

<Label Content="test_t" Name="label2"  />

is shown as "testt" (with the underscore not shown).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

感性 2024-12-18 18:49:25

标签支持助记符(即您可以使用 ctrl+(key) 为其提供焦点)。您可以使用下划线定义助记键。

http://www.charlespetzold.com/blog/2006/01/061004.html

如果您想看到下划线,请将单下划线替换为双下划线。

Labels support mnemonics (i.e. you can use ctrl+(key) to give them focus). You define the mnemonic key using an underscore.

http://www.charlespetzold.com/blog/2006/01/061004.html

If you want to see underscores, replace single underscores with double underscores.

装迷糊 2024-12-18 18:49:25

这是因为 Label 支持根据其内容定义助记符,这是通过在助记符前添加下划线来完成的(与使用 & 在 Windows 窗体中发生的情况相同) 。

如果您希望显示文字,请使用双下划线:

<Label Content="test__t" Name="label2"  />

This is because Label supports defining a mnemonic based on its content, which is done by prefixing the mnemonic with an underscore (the same thing that happens in Windows Forms with &).

Use a double underscore if you want a literal one to appear:

<Label Content="test__t" Name="label2"  />
萌酱 2024-12-18 18:49:25

我知道我来晚了,但我相信,如果您没有与 TextBox 关联的标签,那么您应该使用 TextBlock。

将控件更改为 TextBlock 可以解决此问题,因为只有 Label 具有助记符支持

I know im late to the party but I believe that if you don't have the Label associated to a TextBox than you should use a TextBlock instead.

Changing your control to a TextBlock solves this issue since only Label has the mnemonic support

风向决定发型 2024-12-18 18:49:25

这种风格可以解决您的问题:

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

This style solves your problem:

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

美人如玉 2024-12-18 18:49:25

我绑定到一个我不想更改的数据源,因此我使用自定义转换器来创建缺少的下划线:

[ValueConversion(typeof(string), typeof(string))]
public class ShowUnderscoreConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) =>
        value is string text ? text.Replace("_", "__") : null;

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => 
        value is string text ? text.Replace("__", "_") : null;
}

I was binding to a data source which I didn't want to change, so I used a custom converter to create the missing underscore:

[ValueConversion(typeof(string), typeof(string))]
public class ShowUnderscoreConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) =>
        value is string text ? text.Replace("_", "__") : null;

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => 
        value is string text ? text.Replace("__", "_") : null;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文