尝试在WPF应用程序中更改属性时开始一个故事板

5
我有一个简单的MVVM应用程序。它包含一个属性,当一个方法正确执行时,我会将其更改为true,如果它不正确,则更改为false。当此属性更改时,在几秒钟内,我希望在我的WPF应用程序的状态栏上显示“Passed”或“Failed”,然后让它淡出。
因此,我已经阅读了StackOverflow并进行了大量的谷歌搜索,但都没有找到答案。我认为我误解了如何构建Storyboard。
在我的StatusBar中,我添加了一个Storyboard,我试图在我的XAML文件开头在中触发它。这样做正确吗?目前我正在使用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中注册这个函数调用:

<UserControl.Resources>

<DataTemplate.Triggers>
</DataTemplate.Triggers>
</UserControl.Resources>

我这样的结构完全错误吗?编译不会成功,并出现以下错误:

A value of type 'BeginStoryboard' cannot be added to a collection or dictionary of type 'SetterBaseCollection'. 

非常感谢您能提供任何帮助、资源或信息。非常感谢。

1个回答

5
这是一个例子。您需要使用触发器来启动故事板。
<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); }
    }
}

我在哪里可以找到 ViewModelBase 的实现? - Guge
如果我记得没错,那是来自MVVM light。在这种情况下,它所做的只是实现INotifyPropertyChanged。 - Phil

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接