Thursday, March 19, 2009

WPF - Controlling Styles With MultiTrigger

Multitriggers allow you to make changes to the appearance of a control when multiple conditions are met. You can create MultiTriggers on controls by adding a System.Windows.MultiTrigger to the Style’s Triggers collection. Then create multiple instances of the System.Windows. Condition class, and add them to the MultiTrigger’s Conditions collection. In the MultiTrigger, set the values of the properties to achieve the desired appearance. In the below given sample I have set Triggers on control when the control gets focus or the mouse is moved over the control

<Trigger Property="IsMouseOver" Value="True">
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="BitmapEffect">
<Setter.Value>
<OuterGlowBitmapEffect GlowColor="Blue" GlowSize="2" />
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsFocused" Value="True">
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="BitmapEffect">
<Setter.Value>
<OuterGlowBitmapEffect GlowColor="Blue" GlowSize="2" />
</Setter.Value>
</Setter>
</Trigger>

By using MultiTriggers I can acheive the same without much of coding.

<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="True" />
<Condition Property="IsFocused" Value="True" />
</MultiTrigger.Conditions>
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="BitmapEffect">
<Setter.Value>
<OuterGlowBitmapEffect GlowColor="Blue" GlowSize="2" />
</Setter.Value>
</Setter>
</MultiTrigger>

No comments: