【问题标题】:how to fade out a data bound text block when the property it is bound to is changed, using MVVM如何在绑定的属性更改时淡出数据绑定文本块,使用 MVVM
【发布时间】:2012-03-26 14:13:13
【问题描述】:

我正在使用 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>

所以当在视图模型中更改 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 模型中有需要代码的地方?

【问题讨论】:

    标签: wpf xaml data-binding animation mvvm


    【解决方案1】:

    您正在使用需要采用样式的 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
    }
    

    【讨论】:

    • 先生,我永远感激你。不幸的是,我还不能给你任何赞成票,但也许有一天......
    【解决方案2】:

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

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

    【讨论】:

    • TextBlock 上没有 TextChanged 事件。绑定声明上的NotifyOnTargetUpdated 是下一个最佳选择。
    【解决方案3】:

    【讨论】:

      猜你喜欢
      • 2016-03-28
      • 1970-01-01
      • 1970-01-01
      • 2012-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-03
      • 1970-01-01
      相关资源
      最近更新 更多