Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable WPF label accelerator key (text underscore is missing)

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

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

like image 994
Richard Morgan Avatar asked Sep 02 '08 21:09

Richard Morgan


3 Answers

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

like image 143
yota Avatar answered Oct 21 '22 02:10

yota


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

<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>
like image 38
denis phillips Avatar answered Oct 21 '22 01:10

denis phillips


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

like image 1
satheesh reddy Avatar answered Oct 21 '22 02:10

satheesh reddy