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

时间:2012-03-26 14:13:13

标签: wpf xaml data-binding animation 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>

所以当在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>

任何帮助将不胜感激。也许在View模型中需要代码?

3 个答案:

答案 0 :(得分: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
}

答案 1 :(得分:0)

这种特定于UI的行为绝对应该在View处理,而不是ViewModel

我建议调查TextChanged事件,看看在那里开始动画

答案 2 :(得分:0)