我想向某些文本(也称为 WPF Run 对象)添加淡入/淡出效果。我不希望它同时切换粗体/非粗体。我希望它在粗体和正常之间动画。这可能吗?
最佳答案
我认为您不能在字体粗细之间设置动画,因为它们就像没有“中间”状态的固定值。我认为最好的选择是有 2 个文本 block (一个普通文本 block 和一个具有相同文本的粗体文本 block ),然后对两者的不透明度进行动画处理,使普通文本 block 淡出,粗体文本 block 淡入。这样,它看起来就像字体正在以动画方式从正常“过渡”到粗体。
<Control>
<Control.Template>
<ControlTemplate>
<DockPanel>
<ToggleButton x:Name="btn" Content="IsBold"/>
<Grid Width="200">
<TextBlock Text="Transition" x:Name="normal" TextAlignment="Center"/>
<TextBlock Text="Transition" FontWeight="Bold" Opacity="0" x:Name="bold" TextAlignment="Center"/>
</Grid>
</DockPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" SourceName="btn" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard Duration="00:00:01">
<DoubleAnimation Storyboard.TargetName="normal" Storyboard.TargetProperty="Opacity" To="0"/>
<DoubleAnimation Storyboard.TargetName="bold" Storyboard.TargetProperty="Opacity" To="1"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard Duration="00:00:01">
<DoubleAnimation Storyboard.TargetName="normal" Storyboard.TargetProperty="Opacity" To="1"/>
<DoubleAnimation Storyboard.TargetName="bold" Storyboard.TargetProperty="Opacity" To="0"/>
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Control.Template>
</Control>
关于c# - 可以通过在WPF中平滑地改变字体粗细来脉冲文本吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13145702/