Wpf 如何使用MVVM在数据绑定文本块绑定到的属性更改时淡出数据绑定文本块
我使用的是MVVM设计模式,不希望代码中有太多代码。用XAML和C#进行编码 当用户保存新记录时,我希望“记录已保存”显示在文本块中,然后消失 这是我想做的事情:Wpf 如何使用MVVM在数据绑定文本块绑定到的属性更改时淡出数据绑定文本块,wpf,xaml,data-binding,animation,mvvm,Wpf,Xaml,Data Binding,Animation,Mvvm,我使用的是MVVM设计模式,不希望代码中有太多代码。用XAML和C#进行编码 当用户保存新记录时,我希望“记录已保存”显示在文本块中,然后消失 这是我想做的事情: <TextBlock Name="WorkflowCreated" Text="Record saved"> <TextBlock.Triggers> <DataTrigger Binding="{Binding Path=NewWorkflowCreated}"> <
<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>
任何帮助都将不胜感激。也许视图模型中存在需要代码的行为?这种特定于UI的行为肯定应该在
视图中处理,而不是在视图模型中处理
我建议查看TextChanged
事件,看看如何启动其中的动画您使用的是需要样式的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
}
公共类TestViewModel:INotifyPropertyChanged
{
创建私有bool_newworkflow;
创建公共bool newworkflow
{
获取{return\u newWorkflowCreated;}
集合{
_newWorkflowCreated=值;
PropertyChanged(这是新PropertyChangedEventArgs(“NewWorkflowCreated”);
}
}
#INotifyPropertyChanged的区域实现
公共事件属性更改事件处理程序属性更改;
#端区
}
不是我的博客,但我在这里找到了我想要的东西:
先生,我永远感激您。不幸的是,我还不能给你任何投票,但也许有一天……在TextBlock
上没有TextChanged
事件<绑定声明上的code>NotifyOnTargetUpdated
是次佳选项。