Tuesday, May 3, 2011

What is the advantage of Properties Triggers over Data Triggers in WPF

I try to understand the advantage of Properties Triggers over Data Triggers in WPF. It seems that Properties Triggers can be triggered only by a value that changed in dependency property, and Data Triggers can be triggered both by a value that changed in dependency property, and a value that changed in a .Net object that implement INotifyPropertyChange. So my question is, why not always use Data Triggers?

From stackoverflow
  • Trigger looks at properties in the item you're styling/templating, whereas DataTrigger looks at the current DataContext by default.

    Example:

    <Style TargetType="ListBoxItem">
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <!-- applied when mouse is over the ListBoxItem -->
            </Trigger>
            <DataTrigger Binding="{Binding Name}" Value="Kent">
                <!-- applied when the ListBoxItem's data has a Name property set to "Kent" -->
            </DataTrigger>
        </Style.Triggers>
    </Style>
    

    HTH, Kent

    Andy : So the only property trigger advantage is, that it's a shorter way to get triggered by a value change in the styled item property? since I can bind to the styled item properties using DataTrigger...
    Kent Boogaart : I highly suspect it's more efficient, too.

0 comments:

Post a Comment