禁用 WPF 标签加速键(缺少文本下划线)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40733/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:35:40  来源:igfitidea点击:

Disable WPF label accelerator key (text underscore is missing)

wpfuser-interface

提问by Richard Morgan

I am setting the .Contentvalue of a Label to a string that contains underscores; the first underscore is being interpreted as an accelerator key.

我将.ContentLabel的值设置为包含下划线的字符串;第一个下划线被解释为加速键。

Without changing the underlying string (by replacing all _with __), is there a way to disable the accelerator for Labels?

在不更改底层字符串的情况下(通过将全部替换___),有没有办法禁用标签的加速器?

回答by yota

If you use a TextBlock as the Content of the Label, its Text will not absorb underscores.

如果使用 TextBlock 作为标签的内容,则其文本不会吸收下划线。

回答by denis phillips

You could override the RecognizesAccessKey property of the ContentPresenter that is in the default template for the label. For example:

您可以覆盖位于标签默认模板中的 ContentPresenter 的 RecognizesAccessKey 属性。例如:

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

回答by satheesh reddy

Use a <TextBlock> ... </TextBlock>instead of <Label> ... </Label>to print the exact text, which is having underscores.

使用 a<TextBlock> ... </TextBlock>而不是<Label> ... </Label>打印带有下划线的确切文本。

回答by satheesh reddy

Why not like this?

为什么不喜欢这个?

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