8

I don't understand why WPF allows me to write both

<Grid>
 <Grid.Triggers>
  <DataTrigger Binding="{Binding HasNeverBeenSeen}" Value="true">
   <Setter Property="Background" Value="Red"/>
  </DataTrigger>
 </Grid.Triggers>
</Grid>

and

<Grid>
 <Grid.Style>
  <Style TargetType="{x:Type Grid}">
   <Style.Triggers>
    <DataTrigger Binding="{Binding HasNeverBeenSeen}" Value="true">
     <Setter Property="Background" Value="Red"/>
    </DataTrigger>
   </Style.Triggers>
  </Style>
 </Grid.Style>
</Grid>

but only the second seems to work. Why is there a Triggers tag to Grid element if we must use a Style?

Thanks

1
  • The FrameworkElement.Triggers collection only allows for EventTriggers, whereas the Style.Triggers collection allows for any Trigger types.
    – Fede
    Jul 9, 2014 at 17:50

1 Answer 1

5

Short answer to your question is because this is how it is designed by WPF team.

FrameworkElement.Triggers can only have EventTriggers although property is collection of TriggerBase. It's also clearly stated on MSDN page:

Note that the collection of triggers established on an element only supports EventTrigger, not property triggers (Trigger). If you require property triggers, you must place these within a style or template and then assign that style or template to the element either directly through the Style property, or indirectly through an implicit style reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.