WPF 中不显示下划线

发布于 2024-10-19 03:08:33 字数 93 浏览 7 评论 0原文

在我的整个应用程序中,我有一些未显示的下划线 (_)。

这是由于访问器。但我怎样才能禁用它呢?应用广泛?我的标签、文本框上没有它们……

谢谢

On my whole application, I've some underscores (_) which are not displayed.

It's due to the accessor. But how can I disable it? Application wide? I don't have them on labels, textboxes, ...

Thank you

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

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

发布评论

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

评论(5

铜锣湾横着走 2024-10-26 03:08:33

要为所有标签全局禁用下划线,您可以覆盖标签的默认模板,如下所示:

<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) 中,您的标签将不再识别下划线。

To disable underscores globally for all labels you can override the default template for labels like this:

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

It differs from the default template in this line: RecognizesAccessKey="False".

Put this style in global resources of your application (App.xaml) and your labels will not recognize underscores anymore.

故人爱我别走 2024-10-26 03:08:33

使用两个下划线:

name = "__something";

Use two underscores:

name = "__something";
装纯掩盖桑 2024-10-26 03:08:33

一种简单的解决方案是不使用

One easy solution is to not use <Label>. <TextBox> doesn't mess with underscores.

左耳近心 2024-10-26 03:08:33

您是否尝试过加倍下划线?

Have you tried doubling the underscores?

凉墨 2024-10-26 03:08:33

我遇到了同样的问题 Button & Label 控制内容中下划线不显示的位置。我还希望在不改变按钮或标签的外观和感觉的情况下进行修复。我从 Stackoverflow 上的这个问题中得到了这个想法。我尝试使用双下划线,但是当您尝试访问标签或按钮的内容时,它遇到了问题,您现在必须将双下划线转换为单下划线。

我使用通过样式应用于 ContentControl 的 DataTemplate 实现了该解决方案。我为 ContentPresenter 设置了 RecognizesAccessKey="False"。 Button 控件派生自具有 Content 属性的 ContentControl。因此,我们必须使用 BasedOn 属性将样式应用于它们,如代码所示。

以下是修复前后的 UI 外观:
输入图片这里的描述

这是MainWindow.xaml要测试的代码:

<Window x:Class="ContentControlWithUnderscore.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" SizeToContent="Height" Width="300">

    <StackPanel>
        <TextBlock Background="LightBlue" >Original Text:</TextBlock>
        <StackPanel>
            <TextBlock Text="Some_Text"></TextBlock>
            <TextBlock Text="Some__Text"></TextBlock>
            <TextBlock Text="Som_e_Text"></TextBlock>
        </StackPanel>

        <TextBlock Background="LightBlue">Original Problem:</TextBlock>

        <StackPanel >
            <Button Content="Some_Text"></Button>
            <Button Content="Some__Text"></Button>
            <Button Content="Som_e_Text"></Button>
            <Label Content="Some_Text"></Label>
            <Label Content="Some__Text"></Label>
            <Label Content="Som_e_Text"></Label>
        </StackPanel>

        <TextBlock Background="LightBlue">After Fix:</TextBlock>

        <StackPanel >
            <StackPanel.Resources>
                <Style TargetType="ContentControl">
                    <Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                               <ContentPresenter RecognizesAccessKey="False" Content="{Binding Path=Content, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentControl}}}"></ContentPresenter>
                              </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
                <!-- Inherit the ContentControl style for Button -->
                <Style TargetType="Button" BasedOn="{StaticResource {x:Type ContentControl}}"></Style>
                <!-- Inherit the ContentControl style for Label -->
                <Style TargetType="Label" BasedOn="{StaticResource {x:Type ContentControl}}"></Style>
            </StackPanel.Resources>

            <Button Content="Some_Text"></Button>
            <Button Content="Some__Text"></Button>
            <Button Content="Som_e_Text"></Button>
            <Label Content="Some_Text"></Label>
            <Label Content="Some__Text"></Label>
            <Label Content="Som_e_Text"></Label>
        </StackPanel>
    </StackPanel>
</Window>

I ran into the same problem for Button & Label Control where the underscore inside the content was not displayed. I also want the fix without changing the look and feel of the Button or Label. I got the idea from this question on Stackoverflow. I tried using doubling the underscores, but it had its issues when you tried to access the content of the label or button you now have to convert double underscore to single.

I implemented the solution using DataTemplate applied through style to ContentControl. I set RecognizesAccessKey="False" for the ContentPresenter.As both Label & Button control derive from ContentControl with Content Property. So We have to apply the style to them using the BasedOn attribute as shown in the code.

Here is how the UI looks before and after the fix:
enter image description here

Here is the code for MainWindow.xaml to test:

<Window x:Class="ContentControlWithUnderscore.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" SizeToContent="Height" Width="300">

    <StackPanel>
        <TextBlock Background="LightBlue" >Original Text:</TextBlock>
        <StackPanel>
            <TextBlock Text="Some_Text"></TextBlock>
            <TextBlock Text="Some__Text"></TextBlock>
            <TextBlock Text="Som_e_Text"></TextBlock>
        </StackPanel>

        <TextBlock Background="LightBlue">Original Problem:</TextBlock>

        <StackPanel >
            <Button Content="Some_Text"></Button>
            <Button Content="Some__Text"></Button>
            <Button Content="Som_e_Text"></Button>
            <Label Content="Some_Text"></Label>
            <Label Content="Some__Text"></Label>
            <Label Content="Som_e_Text"></Label>
        </StackPanel>

        <TextBlock Background="LightBlue">After Fix:</TextBlock>

        <StackPanel >
            <StackPanel.Resources>
                <Style TargetType="ContentControl">
                    <Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                               <ContentPresenter RecognizesAccessKey="False" Content="{Binding Path=Content, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentControl}}}"></ContentPresenter>
                              </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
                <!-- Inherit the ContentControl style for Button -->
                <Style TargetType="Button" BasedOn="{StaticResource {x:Type ContentControl}}"></Style>
                <!-- Inherit the ContentControl style for Label -->
                <Style TargetType="Label" BasedOn="{StaticResource {x:Type ContentControl}}"></Style>
            </StackPanel.Resources>

            <Button Content="Some_Text"></Button>
            <Button Content="Some__Text"></Button>
            <Button Content="Som_e_Text"></Button>
            <Label Content="Some_Text"></Label>
            <Label Content="Some__Text"></Label>
            <Label Content="Som_e_Text"></Label>
        </StackPanel>
    </StackPanel>
</Window>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文