Question

I was looking at the template for a winrt project and it has the following style for the back button:

<VisualStateManager.VisualStateGroups>
    <VisualStateGroup x:Name="CommonStates">
        <VisualState x:Name="Normal" />
        <VisualState x:Name="PointerOver">
            ...
        </VisualState>
        <VisualState x:Name="Pressed">
           ...
        </VisualState>
        <VisualState x:Name="Disabled">
            ...
        </VisualState>
    </VisualStateGroup>
    <VisualStateGroup x:Name="FocusStates">
        <VisualState x:Name="Focused">
            ...
        </VisualState>
        <VisualState x:Name="Unfocused" />
        <VisualState x:Name="PointerFocused" />
    </VisualStateGroup>
</VisualStateManager.VisualStateGroups>

I am assuming the above VisualStates are button States, but I can't figure out where this is being tracked on the button object and how the framework binds the state to the visual state.

I have been looking all around the internet to get a better understanding, but to no avail. Please help me understand how this is all tied up together. I know you can manually go to a specific state from code behind, but there seems to be a convention here that I am missing.

Was it helpful?

Solution

There is no property that lists a controls states.

According to MSDN Control Authors must provide a control contract so that ControlTemplate authors will know what to put in the template.

A control contract has three elements:

  • The visual elements that the control's logic uses.
  • The states of the control and the group each state belongs to.
  • The public properties that visually affect the control.

both the visual element and the states are provided as Class Attributes

[TemplatePart(Name = "XXX", Type = typeof(RepeatButton))]
[TemplatePart(Name = "YYY", Type = typeof(RepeatButton))]
[TemplateVisualState(Name = "Focused", GroupName = "FocusedStates")]
[TemplateVisualState(Name = "Unfocused", GroupName = "FocusedStates")]

you should go over the default Control Styles and Templates I think all the dat you are looking for will be there.

if you have to get the data in run time then you can use Reflection to get a given class Attributes like that :

System.Reflection.MemberInfo info = typeof(MyClass);
object[] attributes = info.GetCustomAttributes(true);

for (int i = 0; i < attributes.Length; i++)
{
  if (attributes[i] is TemplatePart || attributes[i] is TemplateVisualState)
  {
     System.Console.WriteLine(((TemplateVisualState) attributes[i]).Name);
  }   
}

read this MSDN Article it will make things clearer

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top