gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 21:48:00 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com