wpf - 尝试在 WPF 应用程序中更改属性时开始 Storyboard

标签 wpf xaml animation properties storyboard

我有一个简单的 MVVM 应用程序。它包含一个属性,当方法正确执行时,我将其更改为 true,如果现在执行,则更改为 false。当此属性更改时,我想在 WPF 应用程序的状态栏上显示“通过”或“失败”几秒钟,然后让它消失。

所以我已经阅读了 StackOverflow,并用谷歌搜索了很多,但无济于事。我想我误解了我需要如何构建 Storyboard。

在我的状态栏中,我添加了一个 Storyboard,我试图在 XAML 文件的开头在 <UserControl.Resources> 中触发它。这样对吗 ?目前我使用的是 0/1 的虚拟值,我认为正确的做法是使用我可以制作的 BooleanToString 转换器,或者也许有更好的方法?

所以我的状态栏包含:

<StatusBar >
  <StatusBar.Resources>
    <Storyboard x:Key="StatusBar" >
      <DoubleAnimationUsingKeyFrames 
                    Storyboard.TargetProperty="(UIElement.Opacity)" 
                    Storyboard.TargetName="statusBarItem">
        <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
        <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="1"/>
        <EasingDoubleKeyFrame KeyTime="0:0:3" Value="1"/>
        <EasingDoubleKeyFrame KeyTime="0:0:4" Value="0"/>
      </DoubleAnimationUsingKeyFrames>
    </Storyboard>
  </StatusBar.Resources>
</StatusBar>  

我正在尝试注册以在我的 UserControl.Resources 中调用它:










我的结构是否完全倒退?它不会编译,我收到错误:
A value of type 'BeginStoryboard' cannot be added to a collection or dictionary of type 'SetterBaseCollection'. 

任何帮助、资源或信息将不胜感激。非常感谢。

最佳答案

这是一个例子。您需要使用触发器来启动 Storyboard。

<Grid>
    <Grid.DataContext>
        <WpfApplication1:MainViewModel/>
    </Grid.DataContext>

    <Grid.Resources>
        <Style x:Key="statusStyle" TargetType="StatusBar">
            <Setter Property="Opacity" Value="0"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Pulse}" Value="True">
                    <DataTrigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimationUsingKeyFrames 
                                    AutoReverse="True" 
                                    Storyboard.TargetProperty="(UIElement.Opacity)" >
                                    <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                                    <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="1"/>
                                    <EasingDoubleKeyFrame KeyTime="0:0:3" Value="1"/>
                                    <EasingDoubleKeyFrame KeyTime="0:0:4" Value="0"/>
                                </DoubleAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.EnterActions>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Grid.Resources>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition Height="auto"/>
    </Grid.RowDefinitions>

    <StatusBar Style="{StaticResource statusStyle}" 
                       Grid.Row="1" ItemsSource="{Binding Items}" />
    <CheckBox Content="CheckBox" Height="16" 
                      HorizontalAlignment="Left" Margin="41,30,0,0" 
                      IsChecked="{Binding Pulse}" VerticalAlignment="Top" />
</Grid>

查看模型
public class MainViewModel : ViewModelBase
{
    private bool _pulse;

    public MainViewModel()
    {
        Items = new[] {"Passed"};
    }

    public IList<string> Items { get; private set; }

    public bool Pulse
    {
        get { return _pulse; }
        set { Set(()=>Pulse, ref _pulse, value); }
    }
}

关于wpf - 尝试在 WPF 应用程序中更改属性时开始 Storyboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11031895/

相关文章:

c# - 让文本与 TextBlock 的顶部齐平(不是 VerticalAlignment,我保证!)

wpf - 如果满足特定数据条件,如何为 DataGrid 单元格背景设置动画?

jsf - PrimeFaces 状态栏是静态的而不是动画的

c# - 如何在不使用 MVVM 的情况下绑定(bind) DependencyProperty

c# - 更改集合时触发方法

c# - Wpf ComboBox 选定项对齐

silverlight - 带有复选框和流向的 Silverlight 中的 Stage 效果

javascript - 如何在 qml 中为多个对象设置动画

c# - 如何选择将哪个 ObservableCollection 绑定(bind)到列表框?

c# - 如何使用 wpf 应用程序在 Twitter 上共享数据或消息?