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

Ric*_*gan 57 wpf user-interface

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

如果没有(更换所有改变基础字符串___),是有办法禁用标签加速器?

小智 80

如果使用TextBlock作为Label的内容,则其Text不会吸收下划线.

  • 我只是在我的应用程序中使用了这种方法,它就像一个冠军. (2认同)
  • 如果您想保留已分配的双击事件,则不起作用(TextBlock 没有) (2认同)

den*_*ips 28

您可以覆盖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>
Run Code Online (Sandbox Code Playgroud)

  • 我是唯一一个认为为了禁用加速键而必须编写半页XAML的错误的人吗? (27认同)