How can I get a strikethrough decoration applied to a label via a trigger?

时间:2019-04-16 23:16:49

标签: wpf datatrigger wpf-style

There are a zillion examples of how to do this to a TextBlock, but I need it on a label. I thought the default template for a label included a TextBlock, so I tried this:

<Grid>
  <Grid.Resources>
    <Style TargetType="{x:Type Label}">
      <Style.Triggers>
        <DataTrigger Binding="{Binding Path=Included}" Value="False">
          <Setter Property="TextBlock.TextDecorations" Value="Strikethrough" />
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </Grid.Resources>

  <Label Grid.Column="0" BorderThickness="0,1,0,0" BorderBrush="White"
         Background="Transparent" Padding="5,0,5,0"
         Content="{Binding Path=BillingDefinitionId}"
         IsEnabled="{Binding Path=Included}" />
</Grid>

The effect I am going for is when Included is false, the label should be disabled and have strikethrough text. But this is not working for the strikethrough bit.

1 个答案:

答案 0 :(得分:0)

If I explicity declare the TextBlock, then it seems to work:

<Grid.Resources>
  <Style TargetType="{x:Type TextBlock}">
    <Style.Triggers>
      <DataTrigger Binding="{Binding Path=Included}" Value="False">
        <Setter Property="TextDecorations" Value="Strikethrough" />
      </DataTrigger>
    </Style.Triggers>
  </Style>
</Grid.Resources>

<Label Grid.Column="0" BorderThickness="0,1,0,0" BorderBrush="White"
       Background="Transparent" Padding="5,0,5,0"
       HorizontalContentAlignment="Right" VerticalContentAlignment="Center"
       IsEnabled="{Binding Path=Included}">
  <TextBlock Text="{Binding Path=BillingDefinitionId}" />
</Label>

I suppose that's fairly simple, not sure why it didn't occur to me prior to posting, but it seems a bit overkill if the default template is a TextBlock… Regardless, it works.