wpf - 如何使用 MVVM 在更改绑定(bind)到的属性时淡出数据绑定(bind)文本 block

标签 wpf xaml data-binding animation mvvm

我正在使用 MVVM 设计模式,不希望我的代码中有太多代码。使用 XAML 和 C# 进行编码。

当用户保存新记录时,我希望“记录已保存”出现在文本 block 中然后消失。

这是我想从事的工作:

<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>

因此,当 View 模型中的 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 模型中需要代码?

最佳答案

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

关于wpf - 如何使用 MVVM 在更改绑定(bind)到的属性时淡出数据绑定(bind)文本 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9873766/

相关文章:

c# - 从 WebBrowser 控件中加载的文档中获取标题

WPF - 不再使用 <?Mapping > 了吗?

c# - 如何使用匿名 LINQ 结果填充 DataTable

java数据绑定(bind)到gui,就像在c#中一样

c# - 使用反射将所有依赖属性重置回默认值?

wpf - 样式 WPF 扩展器图标,如 TreeView 扩展器

c# - ComboBox 绑定(bind)中断对 SelectedItem 中对象的更改

xaml - 在 xamarin.forms 中设计图片库

c# - 通过 MVVM 将 View 输出到另一个到另一个

windows - 运行标签不支持 WP7 中的数据绑定(bind)?