wpf - 从代码隐藏(xaml.cs)开始 Storyboard,而不是从 View 模型 MVVM

标签 wpf xaml mvvm storyboard code-behind

在 WPF 中,我为边框设置了以下样式:

<Style TargetType="Border" x:Key="BorderBlinking">
    <Style.Triggers>
        <DataTrigger Binding="{Binding PopupBlinking}" Value="True">
            <DataTrigger.EnterActions>
                <BeginStoryboard>
                    <Storyboard>                                
                        <DoubleAnimation Storyboard.TargetProperty="Opacity"
                                         To="0" AutoReverse="True" Duration="0:0:0.5" SpeedRatio="3" RepeatBehavior="3x" />
                    </Storyboard>
                </BeginStoryboard>
            </DataTrigger.EnterActions>
            <DataTrigger.ExitActions>
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetProperty="Opacity"
                                         To="1" AutoReverse="True" Duration="0:0:0.5" />
                    </Storyboard>
                </BeginStoryboard>
            </DataTrigger.ExitActions>
        </DataTrigger>
    </Style.Triggers>
</Style>

我像这样附加到边界:
<Border Grid.Row="2" x:Name="popup" 
        Style="{StaticResource BorderBlinking}"
        CornerRadius="10,10,0,0" Height="25" Margin="0"
        HorizontalAlignment="Center" Width="Auto"
        VerticalAlignment="Center"
        BorderBrush="DarkBlue" BorderThickness="1"
        Background="AntiqueWhite">
    <StackPanel  Orientation="Horizontal" HorizontalAlignment="Center">
        <Image Source="Common.Images;component/Images/Info.png" Height="20" Width="20" Stretch="Fill"/>
        <TextBlock Margin="5" VerticalAlignment="Center" HorizontalAlignment="Left" 
       Background="Transparent" FontSize="12"><Run Text="this is a custom popup"/></TextBlock>
    </StackPanel>
</Border>

然后从我后面的代码(不是 View 模型)开始,我想开始 Storyboard。我知道如何通过绑定(bind)到数据触发器的属性“PopupBlinking”(如上例中的)从 View 模型启动它,但现在我需要知道如何从代码隐藏(而不是 View 模型)启动它。

我修改了上面的代码并在下面完成:
        <Storyboard x:Key="Blink" >
            <DoubleAnimation Storyboard.TargetProperty="Opacity" 
                                                 To="0" AutoReverse="True" Duration="0:0:0.5" SpeedRatio="3" RepeatBehavior="3x" />
            <DoubleAnimation Storyboard.TargetProperty="Opacity"
                                                 To="1" AutoReverse="True" Duration="0:0:0.5" />
        </Storyboard>

并从代码隐藏:
        Storyboard sb = Resources["Blink"] as Storyboard;            
        sb.Begin(this.popup);

这是正确的方法吗?

最佳答案

您可以像这样直接启动动画:

popup.BeginAnimation(UIElement.OpacityProperty,
    new DoubleAnimation
    {
        To = 0,
        Duration = TimeSpan.FromSeconds(0.5),
        AutoReverse = true,
        RepeatBehavior = RepeatBehavior.Forever
    });

关于wpf - 从代码隐藏(xaml.cs)开始 Storyboard,而不是从 View 模型 MVVM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45321169/

相关文章:

wpf - WPF 中选定的文本颜色

wpf - 创建仅适用于设计时的属性

javascript - Kendo UI MVVM 不明确的行为

c# - Prism Xamarin Forms INavigationService GoBackAsync 不返回上一页

c# - 在另一个窗口中监听 MediaEnded 事件

c# - 从自己的后台线程更新绑定(bind)控件

c# - 从数据网格中获取对象类型

wpf - 具有混合类型和分类子 TreeViewItems 的数据绑定(bind) WPF TreeView

c# - XamlReader.Load() 编码

c# - 如何使用 MVVM WPF 架构创建自定义用户控件