【问题标题】:Start a storyboard from code-behind (xaml.cs), not from view model MVVM从代码隐藏(xaml.cs)开始故事板,而不是从视图模型 MVVM
【发布时间】:2017-07-26 23:14:19
【问题描述】:

在 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>

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

我已经修改了上面的代码并在下面完成了:

        <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);

这是正确的做法吗?

【问题讨论】:

  • 这意味着您不需要整个 DataTrigger 的东西?并且只想在 Border 元素上启动 Opacity 动画?
  • 是的,我已经修改了代码,请参阅我的更新。这是正确的做法吗?

标签: wpf xaml mvvm storyboard code-behind


【解决方案1】:

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

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

【讨论】:

  • 太棒了!我喜欢它。
猜你喜欢
  • 1970-01-01
  • 2015-03-15
  • 1970-01-01
  • 1970-01-01
  • 2011-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多