可以通过在WPF中平滑地改变字体重量来脉冲文本吗?

时间:2012-10-30 18:46:00

标签: c# wpf animation text

我想为某些文本添加淡入/淡出效果(也称为WPF Run对象)。我不希望它同时切换Bold / notBold。我想让它在Bold和normal之间制作动画。这可能吗?

1 个答案:

答案 0 :(得分:2)

我认为你不能在Fontweights之间制作动画,因为它们就像没有“中间”状态的固定值。我认为最好的选择是拥有2个Textblocks(一个正常一个和一个Bold一个具有相同的文本),然后动画两者的不透明度,使正常的淡出和Bold的淡入淡出。这样看起来就像字体正在以动画的方式从正常“转变”为粗体。

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