当文本在 TextBlock 中换行时,ActualHeight 不正确 [英] When text wraps within TextBlock, ActualHeight is incorrect
问题描述
我有一个 TextBlock
,周围有一个 Border
,它位于 Canvas
内,我用它作为动画的一部分自定义控件.块从屏幕底部滑入图像顶部.我正在尝试使用 TextBlock
的 ActualHeight
来确定将它移到页面上的距离,但是当文本太多以至于它包装成两行时,ActualHeight
返回的大小与只有一行的大小相同.
I have a TextBlock
with a Border
around it that's inside of a Canvas
that I'm using to animate it as part of a custom control. The block slides in from the bottom of the screen over the top of the image. I'm trying to use the ActualHeight
of the TextBlock
to determine how far to move it onto the page, but when there is so much text that it wraps to two lines, the ActualHeight
returns the same size as though there was a single line.
文本块:
<DataTemplate DataType="{x:Type contentTypes:BusinessAdText}" x:Key="BusinessAdTextTemplate">
<Border Background="#a9a9a975"
Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type Canvas}}, Path=ActualWidth}">
<TextBlock Margin="20" Text="{Binding Text}"
TextWrapping="Wrap">
</TextBlock>
</Border>
</DataTemplate>
应用了具有画布的这种样式:
This style is applied which has the canvas:
<Style TargetType="local:BusinessAd">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:BusinessAd">
<Border Background="Transparent">
<Canvas ClipToBounds="True">
<ContentPresenter x:Name="PART_Content"
VerticalAlignment="Center"
HorizontalAlignment="Center" />
</Canvas>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
BusinessAd.cs 的代码有:
Code behind for BusinessAd.cs has:
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
_contentPart = GetTemplateChild("PART_Content") as FrameworkElement;
}
然后只使用一个简单的 DoubleAnimation 我将它移动到屏幕上:
Then just using a simple DoubleAnimation I move it onto the screen:
if (_contentPart != null && _isLoaded)
{
_storyboard.Stop();
vAnimation.From = ActualHeight;
vAnimation.To = ActualHeight - _contentPart.ActualHeight;
//_contentPart.ActualHeight returns 46.something no matter how much text is there
vAnimation.Duration = new Duration(TimeSpan.FromSeconds(Duration));
if (_storyboard.Children.Count == 0)
{
_storyboard.Children.Add(vAnimation);
Storyboard.SetTargetProperty(vAnimation, new PropertyPath("(Canvas.Top)"));
Storyboard.SetTarget(vAnimation, _contentPart);
}
_storyboard.Begin();
}
推荐答案
在检查 ActualHeight
之前必须调用 UpdateLayout()
:
You have to call UpdateLayout()
before checking ActualHeight
:
if (_contentPart != null && _isLoaded)
{
_storyboard.Stop();
UpdateLayout();
vAnimation.From = ActualHeight;
vAnimation.To = ActualHeight - _contentPart.ActualHeight;
//_contentPart.ActualHeight returns 46.something no matter how much text is there
vAnimation.Duration = new Duration(TimeSpan.FromSeconds(Duration));
if (_storyboard.Children.Count == 0)
{
_storyboard.Children.Add(vAnimation);
Storyboard.SetTargetProperty(vAnimation, new PropertyPath("(Canvas.Top)"));
Storyboard.SetTarget(vAnimation, _contentPart);
}
_storyboard.Begin();
}
这篇关于当文本在 TextBlock 中换行时,ActualHeight 不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!