首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >文本块高度的DoubleAnimation

文本块高度的DoubleAnimation
EN

Stack Overflow用户
提问于 2018-01-19 22:12:25
回答 2查看 172关注 0票数 0

我有一个文本块,它应该只显示2行的文本,而它是未选中的。一旦它被选中,我希望它能够顺利地扩展。

我开始的时候是这样的:

代码语言:javascript
复制
<BeginStoryboard>
  <Storyboard>
    <DoubleAnimation
      Storyboard.TargetName="Second" 
      Storyboard.TargetProperty="(TextBlock.MaxHeight)"
      To="50.0" Duration="0:0:0.5" />
  </Storyboard>
</BeginStoryboard>

但这里的问题是,我不知道文本有多大。

EN

回答 2

Stack Overflow用户

发布于 2018-01-19 22:23:07

您应该能够改用From='0',这将以0值开始动画,并以MaxHeight的值结束。但是,这会产生一个不同的问题,因为MaxHeight默认为无穷大,这会使动画太快。如下所示:

代码语言:javascript
复制
<BeginStoryboard>
  <Storyboard>
    <ObjectAnimationUsingKeyframes
      Storyboard.TargetName='Second'
      Storyboard.TargetProperty='(TextBlock.MaxHeight)'>
      <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{Binding TargetName=Second, Path=ActualHeight}" />
    </ObjectAnimationUsingKeyframes>
    <DoubleAnimation
      Storyboard.TargetName="Second" 
      Storyboard.TargetProperty="(TextBlock.MaxHeight)"
      From="0" Duration="0:0:0.5" />
  </Storyboard>
</BeginStoryboard>
票数 0
EN

Stack Overflow用户

发布于 2018-01-19 22:41:27

您可以使用DoubleAnimation来实现这一点。我已经在一个示例应用程序中实现了这一点。

代码语言:javascript
复制
<Window.Resources>
    <Storyboard x:Key="OnGotFocus">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetName="textBox" Storyboard.TargetProperty="(FrameworkElement.Height)">
            <EasingDoubleKeyFrame KeyTime="0:0:2">
                <EasingDoubleKeyFrame.Value>
                    <System:Double>NaN</System:Double>
                </EasingDoubleKeyFrame.Value>
            </EasingDoubleKeyFrame>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
    <Storyboard x:Key="OnLostFocus">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetName="textBox" Storyboard.TargetProperty="(FrameworkElement.Height)">
            <EasingDoubleKeyFrame KeyTime="0">
                <EasingDoubleKeyFrame.Value>
                    <System:Double>NaN</System:Double>
                </EasingDoubleKeyFrame.Value>
            </EasingDoubleKeyFrame>
            <EasingDoubleKeyFrame KeyTime="0:0:2" Value="30" />
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
</Window.Resources>
<Window.Triggers>
    <EventTrigger RoutedEvent="UIElement.GotFocus" SourceName="textBox">
        <BeginStoryboard Storyboard="{StaticResource OnGotFocus}" />
    </EventTrigger>
    <EventTrigger RoutedEvent="UIElement.LostFocus" SourceName="textBox">
        <BeginStoryboard x:Name="OnLostFocus_BeginStoryboard" Storyboard="{StaticResource OnLostFocus}" />
    </EventTrigger>
</Window.Triggers>

textbox的代码应该是:

代码语言:javascript
复制
<TextBox x:Name="textBox"
         Height="30"
         HorizontalAlignment="Right"
         Text="Hello World" />

这将使textbox在聚焦时立即动画到指定的高度。我已经添加了一个动画,当它失去焦点时也会折叠它。

希望这对你有帮助。

票数 -3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48343006

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档