XAML ColorAnimation影响两个具有相同类型WPF的不同对象

iqih9akk  于 2023-04-09  发布在  其他
关注(0)|答案(1)|浏览(112)

我有两组完全不同的控件对象,它们的类型相同,在ControlTemplate中定义了它们的ColorAnimation。奇怪的是,当ColorAnimation的触发器在其中一个中激活时,它也会以某种方式在每个具有相同类型的对象中激活。它只发生在ColorAnimation中,我已经尝试了DoubleAnimation,例如,它按预期工作。
下面是源代码:
ToggleButton的风格

<Style TargetType="ToggleButton" x:Key="ToggleButtonStyle">
        <Setter Property="Width" Value="50"/>
        <Setter Property="Height" Value="20"/>
        <Setter Property="FontSize" Value="15"/>
        <Setter Property="Cursor" Value="Hand"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ToggleButton">
                    <ControlTemplate.Resources>
                        <SolidColorBrush x:Key="HoverBrush" Color="{Binding Source={StaticResource MainHoverColor}, Path=Color}"/>
                        <SolidColorBrush x:Key="BorderBackground" Color="{Binding Source={StaticResource TogglerBackground}, Path=Color}"/>
                    </ControlTemplate.Resources>
                    <Border 
                        x:Name="PART_Border"
                        Background="{StaticResource BorderBackground}"
                        Width="{TemplateBinding Width}"
                        Height="{TemplateBinding Height}"
                        Tag="{TemplateBinding Tag}"
                        CornerRadius="10">
                        <Button 
                            x:Name="PART_Toggler" 
                            HorizontalAlignment="Left"
                            Width="{TemplateBinding FontSize}"
                            Height="{TemplateBinding FontSize}"
                            IsHitTestVisible="False"
                            Margin="3 0 0 0">
                            <Button.Template>
                                <ControlTemplate>
                                    <Ellipse Fill="White"/>
                                </ControlTemplate>
                            </Button.Template>
                        </Button>
                    </Border>
                    <ControlTemplate.Triggers>
                        <EventTrigger RoutedEvent="Checked">
                            <EventTrigger.Actions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <ThicknessAnimation 
                                        Duration="0:0:0.200" 
                                        Storyboard.TargetName="PART_Toggler" 
                                        Storyboard.TargetProperty="Margin"
                                        To="32,0,0,0"/>
                                    </Storyboard>
                                </BeginStoryboard>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <ColorAnimation
                                            Duration="0:0:0.200" 
                                        Storyboard.TargetName="PART_Border" 
                                        Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
                                        To="Red"/>
                                    </Storyboard>
                                </BeginStoryboard>
                            </EventTrigger.Actions>
                        </EventTrigger>
                        <EventTrigger RoutedEvent="Unchecked">
                            <EventTrigger.Actions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <ThicknessAnimation 
                                        Duration="0:0:0.200" 
                                        Storyboard.TargetName="PART_Toggler" 
                                        Storyboard.TargetProperty="Margin"
                                        To="3,0,0,0"/>
                                    </Storyboard>
                                </BeginStoryboard>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <ColorAnimation
                                            Duration="0:0:0.200" 
                                        Storyboard.TargetName="PART_Border" 
                                        Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
                                        To="Black"/>
                                    </Storyboard>
                                </BeginStoryboard>
                            </EventTrigger.Actions>
                        </EventTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

问题是这样发生的:Screenshot GIF

klh5stk1

klh5stk11#

这是一个典型的错误,但你无意中改变了BorderBackground本身的示例。

<Border
    x:Name="PART_Border"
    Width="{TemplateBinding Width}"
    Height="{TemplateBinding Height}"
    Tag="{TemplateBinding Tag}"
    CornerRadius="10">
    <Border.Background>
        <SolidColorBrush Color="{Binding Source={StaticResource TogglerBackground}, Path=Color}"/>
    </Border.Background>
    ...
</Border>

相关问题