【发布时间】:2020-10-29 16:12:01
【问题描述】:
每当我的视图模型中的属性 TargetValue 设置为特定值 Value1 时,我都会使用数据触发器来启动动画。
但我不希望在程序启动期间出现此动画。防止动画在程序启动时运行的最佳方法是什么?
部分视图模型:
public enum TargetValue
{
Value1,
Value2,
}
public class MainWindowViewModel : INotifyPropertyChanged, IDisposable
{
public TargetValue _targetValue = TargetValue.Value1;
public TargetValue TargetValue
{
get => _targetValue;
set => SetProperty(ref _targetValue, value);
}
部分xaml:
<Border Background="Green" >
<Border.Style>
<Style TargetType="{x:Type Border}">
<Style.Triggers>
<DataTrigger Binding="{Binding TargetValue}" Value="Value1">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetProperty="(UIElement.Opacity)">
<LinearDoubleKeyFrame KeyTime="00:00:0" Value="0.5"/>
<LinearDoubleKeyFrame KeyTime="00:00:3" Value="1"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
</Border>
MainWindow.cs 的一部分:
public partial class MainWindow : Window
{
MainWindowViewModel myViewModel = new MainWindowViewModel();
public MainWindow()
{
InitializeComponent();
DataContext = myViewModel;
}
【问题讨论】:
标签: wpf xaml data-binding