VisualState.Name property

Applies to Windows and Windows Phone

Gets the name of the VisualState.

Syntax


public string Name { get; }


<VisualState x:Name="stateName"/>

XAML Values

stateName

The name of the VisualState. Name is a read-only property. You specify the name of the VisualState by setting the x:Name attribute. An unprefixed string "Name" won't work.

Property value

Type: System.String [.NET] | Platform::String [C++]

The name of the VisualState.

Remarks

Name is a read-only property that you can check with code at run-time to read the value of the x:Name attribute that was applied to a VisualState in a XAML control template. You can't literally set Name because it's read-only, and x:Name can only be set in XAML. The value is available as soon as the XAML that contains the control template and the VisualState definition is loaded.

The need to check the Name of a VisualState at run-time is anticipated to be relatively uncommon, and would only be relevant to advanced scenarios. For the most part, the definition of a VisualState in XAML combined with using VisualStateManager and VisualStateGroup events in code provides you all that you need to define and invoke visual states for a custom or Windows Runtime-defined control.

Examples

This example shows the correct way to name a VisualState, which is done by setting the x:Name attribute. This example creates a VisualStateGroup in the ControlTemplate of a Button called "CommonStates" and adds VisualState objects for the states, "Normal", "Pressed", and "PointerOver". The Button also defines a state called "Disabled" that is in the "CommonStates" named VisualStateGroup, but the example omits it for brevity.


<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
            Pointer is over the button.-->
        <VisualState x:Name="PointerOver">
          <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>


Requirements

Minimum supported client

Windows 8

Minimum supported server

Windows Server 2012

Minimum supported phone

Windows Phone 8.1 [Windows Runtime apps only]

Namespace

Windows.UI.Xaml
Windows::UI::Xaml [C++]

Metadata

Windows.winmd

See also

VisualState
x:Name attribute

 

 

Show:
© 2014 Microsoft