我周围有一个TextBlock
Border
位于Canvas
内部,我用它来设置自定义控件的一部分。块从屏幕底部滑过图像顶部。我正在尝试使用ActualHeight
的{{1}}来确定将其移动到页面上的距离,但是当有太多文本包含到两行时,TextBlock
返回相同的大小,就好像只有一行一样。
的TextBlock:
ActualHeight
应用此样式,其中包含画布:
<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>
BusinessAd.cs背后的代码有:
<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>
然后只使用一个简单的DoubleAnimation,我将它移到屏幕上:
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
_contentPart = GetTemplateChild("PART_Content") as FrameworkElement;
}
答案 0 :(得分:2)
在检查UpdateLayout()
之前,您必须致电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();
}
答案 1 :(得分:0)
我不确定这是否适用于你,但对我来说,Windows.UI.Xaml.Controls
中的文本块需要先加上:
myTextBlock.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
之前,当我刚刚myTextBlock.Measure(new Size());
时,它在没有任何文字换行的情况下有效,但是换行ActualWidth
并且ActualHeight
返回了字/字母的尺寸,具体取决于在WrapWholeWords或Wrap