wpf绑定vivibillty动画
时间: 2023-10-06 16:11:42 浏览: 143
wpf加载动画
要实现WPF绑定Vivibility动画,可以使用以下代码:
```xml
<Style x:Key="AnimatedVisibilityStyle" TargetType="{x:Type FrameworkElement}">
<Style.Triggers>
<Trigger Property="Visibility" Value="Collapsed">
<Setter Property="Opacity" Value="0"/>
<Setter Property="Height" Value="0"/>
</Trigger>
<Trigger Property="Visibility" Value="Visible">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Opacity" To="1" Duration="0:0:0.3"/>
<DoubleAnimation Storyboard.TargetProperty="Height" To="Auto" Duration="0:0:0.3"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Opacity" To="0" Duration="0:0:0.3"/>
<DoubleAnimation Storyboard.TargetProperty="Height" To="0" Duration="0:0:0.3"/>
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
</Style.Triggers>
</Style>
```
然后在需要绑定动画的控件上,应用这个样式,并将控件的Visibility属性绑定到ViewModel中的一个Boolean属性:
```xml
<TextBlock Text="Hello World!"
Style="{StaticResource AnimatedVisibilityStyle}"
Visibility="{Binding IsVisible, Converter={StaticResource BooleanToVisibilityConverter}}"/>
```
其中,IsVisible是一个ViewModel中的Boolean属性,BooleanToVisibilityConverter是一个用于将Boolean类型转换为Visibility类型的Converter。这样,当IsVisible为True时,TextBlock就会以动画的形式显示出来,当IsVisible为False时,TextBlock就会以动画的形式隐藏起来。
阅读全文