当前位置:网站首页>【WPF】VisualStateManager

【WPF】VisualStateManager

2022-04-22 05:18:00 Pistachio 2021

    <Grid Background="Transparent">
        <VisualStateManager.VisualStateGroups>
            <!--VisualStateGroup: State group : Consisting of mutually exclusive states , State groups and state groups are not mutually exclusive ;-->
            <VisualStateGroup x:Name="CommonState">
                <!-- View state (Visual States) Represents the style of a control in a special logical state 、 appearance ;-->
                <VisualState x:Name="CheckedState">
                    <Storyboard>
                        <ColorAnimation Storyboard.TargetProperty="Background.(SolidColorBrush.Color)" To="#A1D6FC" Duration="0:0:0:1"/>
                    </Storyboard>
                </VisualState>
                <VisualState x:Name="UnCheckedState"/>
                <!--Transitions: View transformation , Represents the transition of a control from one view state to another -->
                <VisualStateGroup.Transitions>
                    <VisualTransition GeneratedDuration="0:0:1" To="CheckState"/>
                    <VisualTransition GeneratedDuration="0:0:1" To="UnCheckedState"/>
                </VisualStateGroup.Transitions>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
        <CheckBox Content=" Check box " x:Name="_checkbox">
            <i:Interaction.Behaviors>
                <!--DataStateBehavior: Switch between the two states according to the conditional statement -->
                <ei:DataStateBehavior Binding="{Binding IsChecked,ElementName=_checkbox}" Value="True" TrueState="CheckedState" FalseState="UnCheckedState"/>
            </i:Interaction.Behaviors>
        </CheckBox>
    </Grid>

 

 

 

 

 VisualStateManager And trigger The difference between

  1. Trigger Is the event 、 When a property or bound data changes , Corresponding changes made ; and VisualStateManager  You can freely control the switching of states , And set it at the time of switching , You can specify VisualTransition
  2. about Trigger, The person who defines the template is free to specify what changes should be made when the conditions are met ; And for VisualStateManager, You need the control developer to define different VisualState, Then the person who defines the template follows the Convention ( according to  TemplateVisualStateAttribute  characteristic ) To define styles in different states of the control ;
  3. VisualStateManager Not only support WPF, And it also supports UWP, We can say it's “ Cross platform ” Of , and Trigger stay UWP It's not supported

版权声明
本文为[Pistachio 2021]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210625511550.html