VisualState not called

本文关键字:called not VisualState | 更新日期: 2023-09-27 18:14:40

我有一个带有一些视觉状态的自定义切换按钮。我必须以一种简单的方式重写或更改"已检查"状态。但是状态不是显示的,而是调用的。我做错了什么?

这是我的代码(示例)

[TemplateVisualState(GroupName = CommonStateGroup, Name = ToggleButton.CheckedNormal)]
public partial class ToggleButton : ToggleButton
{
    internal const String CommonStateGroup = "CommonStates";
    internal const String CheckedNormal = "CheckedNormal";
    protected virtual void ChangeVisualState(bool useTransitions)
    {
        if (this.IsChecked.HasValue && this.IsChecked.Value)
        {
            VisualStateManager.GoToState(this, CheckedNormal, useTransitions);
        }
    }
    protected override void OnChecked(RoutedEventArgs e)
    {
        base.OnChecked(e);
        this.ChangeVisualState(true);
    }

和模板

               <ControlTemplate x:Key="myTemplate" TargetType="{x:Type vw:ToggleButton}">
    <Grid Background="Transparent">
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="CommonStates">
                <VisualState x:Name="Normal"/>
                <VisualState x:Name="CheckedNormal">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="CheckedRectangle">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ToggleButtonCheckedBackgroundThemeBrush}"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="BackgroundBorder">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ToggleButtonCheckedBorderThemeBrush}"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ToggleButtonCheckedForegroundThemeBrush}"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
        <Border x:Name="BackgroundBorder" CornerRadius="{TemplateBinding CornerRadius}" BorderBrush="{TemplateBinding BorderBrush}" Margin="3" BorderThickness="{TemplateBinding BorderThickness}">
            <Grid>
                <Rectangle x:Name="CheckedRectangle" StrokeThickness="0"/>
                <Rectangle x:Name="MouseOverRectangle" StrokeThickness="0"/>
                <Border x:Name="BlinkBorder" Background="{TemplateBinding BlinkBrush}" CornerRadius="{TemplateBinding CornerRadius}" Opacity="0"/>
                <ContentControl x:Name="ContentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
            </Grid>
        </Border>
        <Rectangle x:Name="FocusVisualWhite" IsHitTestVisible="False" Opacity="0" StrokeDashOffset="1.5" StrokeEndLineCap="Square" Stroke="{StaticResource FocusVisualWhiteStrokeThemeBrush}" StrokeDashArray="1,1"/>
        <Rectangle x:Name="FocusVisualBlack" IsHitTestVisible="False" Opacity="0" StrokeDashOffset="0.5" StrokeEndLineCap="Square" Stroke="{StaticResource FocusVisualBlackStrokeThemeBrush}" StrokeDashArray="1,1"/>
    </Grid>
</ControlTemplate>

模板是在Blend中创建的,通过点击周围的可视状态,状态是test。

我认为行为被"checked"状态打断了

VisualState not called

我的解决方案:

添加"UncheckedNormal"状态

编辑以下状态:正常:CheckedBorder的可见性=可见

Enabled: Visibility of CheckedBorder = Visible

鼠标悬停:CheckedBorder = collapse

CheckedNormal: Visibility of CheckedBorder = VisibleBackground = CheckedBackgroundBrush

UncheckedNormal: Visibility of CheckedBorder = VisibleBackground = transparent