我有一个简单的MVVM应用程序。它包含一个属性,当方法正确执行时我会将其更改为true,如果现在这样做,我将更改为false。当这个属性发生变化时,我希望在我的WPF应用程序的状态栏上显示“Passed”或“Failed”,然后逐渐消失。
所以我读过StackOverflow,然后用Google搜索,无济于事。我想我误解了如何构建故事板。
在我的StatusBar中,我添加了一个故事板,我试图在我的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'.
非常感谢任何帮助,资源或信息。非常感谢。
答案 0 :(得分: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); }
}
}