views:

120

answers:

1

In WPF custom control I used to implement appearence changing of a custom control based on another property of this custom control with the help of Triggers mechanism, for example when my custom control changes its property AStatus to value Available its background color changes to Green:

<Trigger Property="AStatus" Value="Available">
    <Setter TargetName="PART1" Property="Background" Value="Green"/>
    <Setter TargetName="PART_Backgr" Property="Background" Value="Green"/>
</Trigger> 

But Silverlight lacks of Triggers functionality. And for changing appearence of custom controls in Silverlight the VisualStateManager should be used. But I cannot find the way this condition can be implemented with the help of the VisualStateManager.

How is it possible to implement changing a style of Silverlight custom control when another property of this custom control changes?

+1  A: 

You have two reasonable options:

1) In the backing code for the property "AStatus", which you may want to make a dependency property, switch to a new VisualState using the VisualStateManager. There is not a fully XAML solution like in WPF when using this technique though. Create a VisualState that represents the style/setters that you want to use (much like a trigger).

Here is an example from my blog.

2) If you use Blend 4, you can use the DataStateBehavior to perform the work that could have been done manually (as in option #1). You can download the Blend 4 SDK to make these types of changes using XAML (or a visual designer).

I usually go with #1 though if writing a custom control to minimize dependencies on other assemblies.

WPCoder
Thanks! Could you please specify, regarding the option #1, did you mean switching over to a new VisualState in the Code Behind file of a XAML page, using some Event Handler? If yes, which Event Handler do you prefer to use? If you meant making this switch over in a C# file that contains all the properties of my Custom control (BTW they are dependency properties), let it be MyCustControl.cs, could you please provide some small example of this part of code? +1
rem
In the code behind of the XAML page, in the property AStatus. I've pointed to an example on my blog.
WPCoder
Thank you! It will be a good start for me.
rem