wpf 状态切换

调用

# 调用 

// 切换状态,指定模拟的状态时使用 VisualStateManager.GoToState, 现在指定了控件的状态,使用 VisualStateManager.GoToElementState
VisualStateManager.GoToElementState(this.ColorBorder, name, true);

xaml

 <Grid>

        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <Grid.Resources>
            <!-- 定义切换按钮的样式 -->
            <Style BasedOn="{StaticResource {x:Type RadioButton}}" TargetType="RadioButton">
                <Setter Property="VerticalAlignment" Value="Center" />
                <Setter Property="Margin" Value="24,0,0,12" />
            </Style>
        </Grid.Resources>

        <!--  定义显示的颜色状态  -->
        <Border
            x:Name="ColorBorder"
            Grid.Row="0"
            Margin="12" Padding="8,12"
            Background="Gray">
            <Border.Effect>
                <!-- <BlurEffect KernelType="Gaussian" Radius="3" /> -->
                <DropShadowEffect
                    BlurRadius="5" Opacity=".5" ShadowDepth="5" />
            </Border.Effect>

            <VisualStateManager.VisualStateGroups>

                <!-- 1. 定义状态,下面状态组中定义了三种状态 -->
                <VisualStateGroup Name="BusyStateGroup">

                    <!--  2. 定义三种状态, 每种状态的属性,到达属性的时间要设置为 0, 过渡在 Transitions 中定义  -->
                    <VisualState x:Name="BusyVisualState">
                        <Storyboard>
                            <ColorAnimation
                                Storyboard.TargetName="ColorBorder" Storyboard.TargetProperty="Background.(SolidColorBrush.Color)" To="Pink" />
                        </Storyboard>
                    </VisualState>

                    <VisualState x:Name="FreeVisualState">
                        <Storyboard>
                            <ColorAnimation
                                Storyboard.TargetName="ColorBorder" Storyboard.TargetProperty="Background.(SolidColorBrush.Color)" To="LightGreen" />
                        </Storyboard>
                    </VisualState>

                    <VisualState x:Name="NormalVisualState">
                        <Storyboard>
                            <ColorAnimation
                                Storyboard.TargetName="ColorBorder" Storyboard.TargetProperty="Background.(SolidColorBrush.Color)" To="LightBlue" />
                        </Storyboard>
                    </VisualState>

                    <!--  3. 设置到每个状态的过渡态,一般要设置时长, 可以指定 from -> to 的过渡态,也可以仅指定 to 的过渡态  -->
                    <VisualStateGroup.Transitions>

                        <VisualTransition GeneratedDuration="0:0:1" To="BusyVisualState">
                            <Storyboard>
                                <ColorAnimation
                                    Storyboard.TargetName="ColorBorder" Storyboard.TargetProperty="Background.(SolidColorBrush.Color)" To="Pink" Duration="0:0:1" />
                            </Storyboard>
                        </VisualTransition>

                        <VisualTransition GeneratedDuration="0:0:1" To="FreeVisualState">
                            <Storyboard>
                                <ColorAnimation
                                    Storyboard.TargetName="ColorBorder" Storyboard.TargetProperty="Background.(SolidColorBrush.Color)" To="LightGreen" Duration="0:0:1" />
                            </Storyboard>
                        </VisualTransition>

                        <VisualTransition GeneratedDuration="0:0:1" To="NormalVisualState">
                            <Storyboard>
                                <ColorAnimation
                                    Storyboard.TargetName="ColorBorder" Storyboard.TargetProperty="Background.(SolidColorBrush.Color)" To="LightBlue" Duration="0:0:1" />
                            </Storyboard>
                        </VisualTransition>

                    </VisualStateGroup.Transitions>

                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
        </Border>

        <UniformGrid Grid.Row="1" Columns="1">

            <!--  设置三个按钮切换状态, ToggleButton_OnChecked 中调用状态切换函数  -->

            <RadioButton
                Checked="ToggleButton_OnChecked" Content="Busy" Tag="BusyVisualState" />

            <RadioButton
                Checked="ToggleButton_OnChecked" Content="Free" Tag="FreeVisualState" />

            <RadioButton
                Checked="ToggleButton_OnChecked" Content="Normal" Tag="NormalVisualState" />

        </UniformGrid>

    </Grid>
上一篇
下一篇