Export (0) Print
Expand All

TemplateVisualStateAttribute Class

Specifies that a control can be in a certain state and that a VisualState is expected in the control's ControlTemplate.

System.Object
  System.Attribute
    System.Windows.TemplateVisualStateAttribute

Namespace:  System.Windows
Assembly:  System.Windows (in System.Windows.dll)

'Declaration
<AttributeUsageAttribute(AttributeTargets.Class, AllowMultiple := True)> _
Public NotInheritable Class TemplateVisualStateAttribute _
	Inherits Attribute

The TemplateVisualStateAttribute type exposes the following members.

  NameDescription
Public methodSupported by Silverlight for Windows PhoneTemplateVisualStateAttributeInitializes a new instance of the TemplateVisualStateAttribute class.
Top

  NameDescription
Public propertySupported by Silverlight for Windows PhoneGroupNameGets or sets the name of the group that the state belongs to.
Public propertySupported by Silverlight for Windows PhoneNameGets or sets the name of the state that the control can be in.
Top

  NameDescription
Public methodSupported by Silverlight for Windows PhoneEqualsInfrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.)
Protected methodSupported by Silverlight for Windows PhoneFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneGetHashCodeReturns the hash code for this instance. (Inherited from Attribute.)
Public methodSupported by Silverlight for Windows PhoneGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneMatchWhen overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Protected methodSupported by Silverlight for Windows PhoneMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneToStringReturns a string that represents the current object. (Inherited from Object.)
Top

Control authors that use the parts control model apply this attribute to the class definition of a control class to inform template authors what states the control can be in. The states that are specified by the TemplateVisualStateAttribute are part of the control contract ControlTemplate authors add VisualState objects to the template to specify the appearance of a control when it is in a certain state. States that share the same GroupName are mutually exclusive and are defined in a single VisualStateGroup in the ControlTemplate.

The following example shows the states that are specified for the Button.


<TemplateVisualStateAttribute(Name:="Pressed", GroupName:="CommonStates")> _
<TemplateVisualStateAttribute(Name:="Focused", GroupName:="FocusStates")> _
<TemplateVisualStateAttribute(Name:="Normal", GroupName:="CommonStates")> _
<TemplateVisualStateAttribute(Name:="MouseOver", GroupName:="CommonStates")> _
<TemplateVisualStateAttribute(Name:="Disabled", GroupName:="CommonStates")> _
<TemplateVisualStateAttribute(Name:="Unfocused", GroupName:="FocusStates")> _
Public Class Button
    Inherits ButtonBase

End Class


The following example defines the VisualState objects for the Normal, MouseOver, and Pressed states, which are all in the CommonStates group. The Disabled state and the states in the FocusStates group are omitted to keep the example short. For the entire example, see Customizing the Appearance of an Existing Control by Using a ControlTemplate.


<ControlTemplate TargetType="Button">
  <Border x:Name="RootElement">

    <VisualStateManager.VisualStateGroups>

      <!--Define the states for the common states.
          The states in the VisualStateGroup are mutually exclusive to
          each other.-->
      <VisualStateGroup x:Name="CommonStates">

        <!--The Normal state is the state the button is in
            when it is not in another state from this VisualStateGroup.-->
        <VisualState x:Name="Normal" />

        <!--Change the SolidColorBrush, BorderBrush, to red when the
            mouse is over the button.-->
        <VisualState x:Name="MouseOver">
          <Storyboard>
            <ColorAnimation Storyboard.TargetName="BorderBrush" 
                              Storyboard.TargetProperty="Color" To="Red" />

          </Storyboard>

        </VisualState>

        <!--Change the SolidColorBrush, BorderBrush, to Transparent when the
            button is pressed.-->
        <VisualState x:Name="Pressed">
          <Storyboard >
            <ColorAnimation Storyboard.TargetName="BorderBrush" 
                              Storyboard.TargetProperty="Color" To="Transparent"/>
          </Storyboard>
        </VisualState>
          <!--The Disabled state is omitted for brevity.-->
        </VisualStateGroup>

    </VisualStateManager.VisualStateGroups>


    <Border.Background>
      <SolidColorBrush x:Name="BorderBrush" Color="Black"/>
    </Border.Background>

    <Grid Background="{TemplateBinding Background}" Margin="4">
      <ContentPresenter
        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
        VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
        Margin="4,5,4,4" />

    </Grid>


  </Border>
</ControlTemplate>


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Community Additions

ADD
Show:
© 2014 Microsoft