文本块高度

时间:2018-01-19 14:12:25

标签: wpf

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

我开始使用类似的东西:

<BeginStoryboard>
  <Storyboard>
    <DoubleAnimation
      Storyboard.TargetName="Second" 
      Storyboard.TargetProperty="(TextBlock.MaxHeight)"
      To="50.0" Duration="0:0:0.5" />
  </Storyboard>
</BeginStoryboard>

但问题是,我不知道文字有多大。

2 个答案:

答案 0 :(得分:0)

您应该能够使用0代替MaxHeight,这将以值MaxHeight开始动画,并以ObjectAnimationUsingKeyframes的值结束。但是,这引发了一个不同的问题,因为MaxHeight默认为无穷大,这会使动画太快。在将ActualHeight设置为<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> 的开头添加xmlns:android = "http://schemas.android.com/apk/res/android"可能会解决此问题。像这样:

.config

答案 1 :(得分:-2)

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

<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 x:Name="textBox"
         Height="30"
         HorizontalAlignment="Right"
         Text="Hello World" />

一旦聚焦,文本框就会动画到指定的高度。我已经添加了一个动画来折叠它,当它失去焦点时。

希望这会对你有所帮助。