Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual State Manager on Custom Control

I have built a custom control that extends content control. Within this I have a parts and states model that is working fine.

I then use this as the root of my xaml(placing the code in the templates folder in Blend)

Everything works fine, I can open a new 'DaveControl' and get the functionality that I want.

However, If I then add make some visual states here, such as alertOnScreen and try to use the visualstatemanager it doesn't work.

infact there are no states listed.

The Behaviour in SL3 GotoVisualState works fine though!

How can I get the visual state to work in code?

like image 552
DavidA Avatar asked Jan 13 '10 09:01

DavidA


2 Answers

So the solution is the following: Use Extended Visual State Manager!

ExtendedVisualStateManager.GoToElementState(this.LayoutRoot as FrameworkElement, "OffScreen", true);

This works because it takes a Framework Element... amongst other things. Workaround for VisualStateManager.GoToState not working on Window

like image 135
DavidA Avatar answered Oct 07 '22 16:10

DavidA


Have you added the set of available states as a set of TemplateVisualState attributes on the class? Blend uses these to configure its list of available states.

Your class should generally look like this:-

[TemplateVisualState(Name = "MyGroup1State1", GroupName = "MyGroup1")]
[TemplateVisualState(Name = "MyGroup1State2", GroupName = "MyGroup1")]
[TemplateVisualState(Name = "MyGroup2State1", GroupName = "MyGroup2")]
[TemplateVisualState(Name = "MyGroup2State2", GroupName = "MyGroup2")]
[TemplatePart(...)]
[TemplatePart(...)]
public class MyControl : ContentControl
like image 45
AnthonyWJones Avatar answered Oct 07 '22 18:10

AnthonyWJones