4

When I include an underscore in a label text, to get access key functionality, the label suddenly jumped right by a certain amount of pixels.

Two labels above each other, one which has extra PaddingLeft

When I tried this out in Kaxaml, I realized it was a Style with TargetType="TextBlock" that applied margin, only to the label with the underscore in it.

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <UserControl.Resources>

      <Style TargetType="TextBlock">
          <Setter Property="VerticalAlignment" Value="Center"/>
          <Setter Property="HorizontalAlignment" Value="Left"/>
          <Setter Property="Margin" Value="5,2,0,1" />
      </Style>

  </UserControl.Resources>
<Grid>  
  <Grid.ColumnDefinitions>
    <ColumnDefinition></ColumnDefinition>
  </Grid.ColumnDefinitions>
  <Grid.RowDefinitions>
    <RowDefinition Height="Auto"></RowDefinition>
    <RowDefinition Height="Auto"></RowDefinition>
  </Grid.RowDefinitions>
  <Label Grid.Column="0" Grid.Row="0">Avfallets _Id:</Label>
  <Label Grid.Column="0" Grid.Row="1">Avfallets Id:</Label>
</Grid>

What is the logic here? Is somehow a label not a TextBlock until you add an underscore to it?

nitech
  • 1,822
  • 3
  • 21
  • 35
  • Now that's something you don't see every day. But from I can see is that until the underscore is present the content of the `Label` could be anything, i.e. Image, vector graphic etc ..., However when you assign the underscore it then can only use `TextBlock` as it's content. That would be the outcome of the internal `TemplateSelector`. I think – XAMlMAX Mar 01 '16 at 12:12
  • Thanks for your comment @XAMlMAX. You're referring to `TemplateSelector`. Are you saying this is a bug or is it valid behavior? – nitech Mar 02 '16 at 06:49
  • 2
    I would say it is a valid behavior as you can only have an access keys with an `AccessText` and the `Label` could have anything set as it's content which may not necessarily be accessible with keys, that is an icon . Unless you have a specific reason to use `Label` i.e. the text might change to an icon or something else I would use the `AccessText` which is a lot more light weight, and that is what `Label` will use internally to have the key functionality. – XAMlMAX Mar 02 '16 at 10:30

0 Answers0