当使用MVVM改变绑定的属性时,如何淡出数据绑定文本块

Rob*_*ens 3 data-binding wpf animation xaml mvvm

我正在使用MVVM设计模式,并且不希望我的代码背后有太多代码.在XAML和C#中编码.

当用户保存新记录时,我希望"记录保存"出现在文本块中,然后逐渐消失.

这是我想要的工作:

<TextBlock Name="WorkflowCreated" Text="Record saved">
  <TextBlock.Triggers>
    <DataTrigger Binding="{Binding Path=NewWorkflowCreated}">
       <DataTrigger.EnterActions>
        <BeginStoryboard>
            <Storyboard>
                <DoubleAnimation
                 Storyboard.TargetName="WorkflowCreated" 
                 Storyboard.TargetProperty="(TextBlock.Opacity)"
                 From="1.0" To="0.0" Duration="0:0:3"/>
            </Storyboard>
        </BeginStoryboard>
     </DataTrigger.EnterActions>
  </DataTrigger>
</TextBlock.Triggers>
Run Code Online (Sandbox Code Playgroud)

所以当在viewmodel中更改NewWorkflowCreated时它会触发动画,遗憾的是这不起作用.我也试过这个:

<TextBlock Name="Message" Text="This is a test.">
 <TextBlock.Triggers>
  <EventTrigger RoutedEvent="TextBlock.Loaded">
   <BeginStoryboard>
    <Storyboard>
      <DoubleAnimation
        Storyboard.TargetName="Message" 
        Storyboard.TargetProperty="(TextBlock.Opacity)"
        From="1.0" To="0.0" Duration="0:0:3"/>
    </Storyboard>
   </BeginStoryboard>
  </EventTrigger>
 </TextBlock.Triggers>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)

任何帮助将非常感激.也许在View模型中需要代码?

Phi*_*hil 5

您正在使用需要采用样式的DataTrigger.

<Window.DataContext>
    <WpfApplication2:TestViewModel/>
</Window.DataContext>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Grid.Resources>
        <Style x:Key="textBoxStyle" TargetType="{x:Type TextBlock}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=NewWorkflowCreated}" Value="True">
                    <DataTrigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation
                                    Storyboard.TargetProperty="(TextBlock.Opacity)"
                                    From="1.0" To="0.0" Duration="0:0:3"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.EnterActions>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Grid.Resources>
    <TextBlock Name="WorkflowCreated" Style="{StaticResource textBoxStyle}" Text="Record saved" />
    <Button Content="press me" Grid.Row="1" Click="Button_Click_1"/>
</Grid>

public class TestViewModel : INotifyPropertyChanged
{
    private bool _newWorkflowCreated;
    public bool NewWorkflowCreated
    {
        get { return _newWorkflowCreated; }
        set { 
            _newWorkflowCreated = value;
            PropertyChanged(this, new PropertyChangedEventArgs("NewWorkflowCreated"));
        }
    }


    #region Implementation of INotifyPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion
}
Run Code Online (Sandbox Code Playgroud)