WPF,如何通过属性更改启动StoryBoard

问题描述 投票:0回答:1

我正在尝试使用 PropertyChange 启动 StoryBoard。目标是在属性更改时为背景设置动画。

问题是,如何将属性绑定到EventTrigger?


<Label>
    <Label.Style>
        <Style BasedOn="{StaticResource {x:Type Label}}" TargetType="{x:Type Label}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Label}">
                        <Grid>
                            <Border x:Name="BackgroundBorder" Background="{DynamicResource EfoGreen}" />
                            <ContentPresenter VerticalAlignment="Center" />
                        </Grid>

                        <ControlTemplate.Triggers>
                            <EventTrigger RoutedEvent="????????????">
                                <BeginStoryboard>
                                    <Storyboard>
                                        <DoubleAnimation
                                                    Storyboard.TargetName="BackgroundBorder"
                                                    Storyboard.TargetProperty="Opacity"
                                                    From="0"
                                                    To="1"
                                                    Duration="0:0:2">
                                        </DoubleAnimation>
                                    </Storyboard>
                                </BeginStoryboard>
                            </EventTrigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Label.Style>
</Label>

我正在尝试使用 PropertyChange 启动 StoryBoard。目标是在属性更改时为背景设置动画。

问题是,如何将属性绑定到EventTrigger?

wpf xaml
1个回答
0
投票

要将属性更改绑定到 XAML 中的 EventTrigger,可以使用 Microsoft.Xaml.Behaviors.Wpf 包中的 PropertyChangedTrigger。以下是实现它的方法:

Install-Package Microsoft.Xaml.Behaviors.Wpf

然后,您可以使用 PropertyChangedTrigger 在属性更改时触发故事板。

<Label>
    <Label.Style>
        <Style BasedOn="{StaticResource {x:Type Label}}" TargetType="{x:Type Label}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Label}">
                        <Grid>
                            <Border x:Name="BackgroundBorder" Background="{DynamicResource EfoGreen}" />
                            <ContentPresenter VerticalAlignment="Center" />
                        </Grid>
                        <ControlTemplate.Triggers>
                            <EventTrigger SourceName="YourSourceObject" EventName="YourEvent">
                                <BeginStoryboard>
                                    <Storyboard>
                                        <DoubleAnimation
                                            Storyboard.TargetName="BackgroundBorder"
                                            Storyboard.TargetProperty="Opacity"
                                            From="0"
                                            To="1"
                                            Duration="0:0:2">
                                        </DoubleAnimation>
                                    </Storyboard>
                                </BeginStoryboard>
                            </EventTrigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Label.Style>
    <i:Interaction.Triggers>
        <i:PropertyChangedTrigger Binding="{Binding YourProperty}">
            <i:InvokeCommandAction Command="{Binding YourCommand}" />
        </i:PropertyChangedTrigger>
    </i:Interaction.Triggers>
</Label>
© www.soinside.com 2019 - 2024. All rights reserved.