silverlight基本使用Visual State Manager

代码如下:

<UserControl x:Class="VsmState.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="800" d:DesignWidth="800">

    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.Resources>
            <Style x:Key="ButtonTemplete" targettype="Button">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate targettype="Button">
                            <Border x:Name="border" Width="300" Height="100" Opacity="1" BorderThickness="2" >  
                                <!--状态组合-->
                                <visualstatemanager.VisualStateGroups>
                                    <!--单个状态组合-->
                                    <VisualStateGroup x:Name="commentstate">
                                        <!--设置单个的状态组里不同状态切换时的动画时间-->
                                        <VisualStateGroup.Transitions>
                                            <VisualTransition From="normal" To="MouSEOver" GeneratedDuration="0:0:0.3"></VisualTransition>
                                            <VisualTransition From="MouSEOver" To="normal" GeneratedDuration="0:0:0.2"></VisualTransition>
                                            <VisualTransition From="MouSEOver" To="pressed" GeneratedDuration="0:0:0.2"></VisualTransition>
                                            <VisualTransition From="pressed" To="MouSEOver" GeneratedDuration="0:0:0.2"></VisualTransition>
                                        </VisualStateGroup.Transitions>
                                        <!--单个状态的动画,下面的x:Name不能写错哦,不然无效果-->
                                        <VisualState x:Name="normal"></VisualState>
                                        <VisualState x:Name="MouSEOver" >
                                            <Storyboard>
                                                <DoubleAnimation Storyboard.TargetName="border" Storyboard.TargetProperty="Width" To="600"></DoubleAnimation>
                                                <DoubleAnimation Storyboard.TargetName="border" Storyboard.TargetProperty="Height" To="120"></DoubleAnimation>
                                                <DoubleAnimation Storyboard.TargetName="border" Storyboard.TargetProperty="Opacity" To="0.8"></DoubleAnimation>
                                            </Storyboard>
                                        </VisualState>
                                        <VisualState x:Name="pressed">
                                            <Storyboard>
                                                <DoubleAnimation Storyboard.TargetName="border" Storyboard.TargetProperty="Opacity" To="0.8"></DoubleAnimation>
                                                <ColorAnimation Storyboard.TargetName="BackgroundBrush" Storyboard.TargetProperty="Color" To="LightSkyBlue" />
                                                <ColorAnimation Storyboard.TargetName="BorderBrush" Storyboard.TargetProperty="Color" To="Blue" />

                                            </Storyboard>
                                        </VisualState>
                                    </VisualStateGroup>
                                </visualstatemanager.VisualStateGroups>
                                <!--内容设置.-->
                                <ContentPresenter                                  
                                         HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                         VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                                <!--背景色设置.-->
                                <Border.Background>
                                    <SolidColorBrush x:Name="BackgroundBrush" Color="Gray"/>
                                </Border.Background>
                                <!--边框颜色设置.-->
                                <Border.BorderBrush>
                                    <SolidColorBrush x:Name="BorderBrush" Color="Black"/>
                                </Border.BorderBrush>

                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Grid.Resources>
        <!--button引用ButtonTemplate模板.-->
        <Button  HorizontalAlignment="Center" VerticalAlignment="Center" Content="I'm a Button" Style="{StaticResource ButtonTemplete}"/>

    </Grid>
</UserControl>
效果有点像android里面的选择器Seletor
效果如下:

normal状态:

normal--->>>MouSEOver:

MouSEOver--->>>pressed:

相关文章

如何在Silverlight4(XAML)中绑定IsEnabled属性?我试过简单的...
我正在编写我的第一个vb.net应用程序(但我也会在这里标记c#,...
ProcessFile()是在UIThread上运行还是在单独的线程上运行.如...
我从同行那里听说,对sharepoint的了解对职业生涯有益.我们不...
我正在尝试保存一个类我的类对象的集合.我收到一个错误说明:...
我需要根据Silverlight中的某些配置值设置给定控件的Style.我...