Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Style.Triggers in Silverlight 4.0?

Having switched from WPF to Silverlight 4.0 I am stuck at something quite basic. There seem to be no way I could use a Trigger within my styles. How is this gonna work?

e.g. here I have created a DataGrid ColumnHeader style within my ResourceDictionary:

<Style x:Key="DataGridColumnHeaderStyle" TargetType="sdk:DataGridColumnHeader"  >
        <Setter Property="Background" Value="#88800080" />
        <Setter Property="Foreground" Value="White" />
        <Style.Triggers>
            <Trigger Property="SortDirection" Value="{x:Null}">
                <Setter Property="Background" Value="{DynamicResource DataGridHeaderBackgroundBrush}" />
                <Setter Property="BorderBrush"  Value="Transparent" />
            </Trigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsMouseOver" Value="True" />
                    <Condition Property="SortDirection" Value="{x:Null}" />
                </MultiTrigger.Conditions>
                <Setter Property="Background" Value="{StaticResource DataGridHeaderMouseOverBackgroundBrush}" />
                <Setter Property="BorderBrush" Value="{StaticResource DataGridHeaderBorderBrush}" />
            </MultiTrigger>
...

Some google search attempts from early 2009 claim to use Converters instead, but I am completely stuck with this. I would really appreciate if someone could give me a hint about how to do it.

like image 267
Houman Avatar asked Apr 27 '11 21:04

Houman


1 Answers

Expression Blend added some support for WPF type Triggers to Silverlight. This blog post explains this more.

But in short, you don't get the same value precedence with those triggers as you do with WPF. Meaning, a Style trigger can override a setting made explicitly on an element.

Silverlight uses the VisualStateManager concept to "theme" or customize the look of a control. It effectively forces you to define "fixed" states, such as Hover (i.e. IsMouseOver = true) or Pressed. You can then apply animations when entering or exiting these states. So you can say, animate the background brush when hovering to give a hovered look.

The VisualStateManager approach makes it much easier to provide tooling around customizing the look of a control. This is shown in more detail in this blog post.

So in short, you won't be able to translate it one-to-one. You'll find there are many things like this. For example, there is no IsMouseOver property on UIElement as there is in WPF.

like image 87
CodeNaked Avatar answered Nov 15 '22 08:11

CodeNaked