2

我在它周围有一个TextBlocka ,它在我用来作为自定义控件的一部分对其进行动画处理的 a 的内部。块从屏幕底部滑入图像顶部。我正在尝试使用来确定将其移动到页面上的距离,但是当文本太多以至于它换成两行时,返回的大小就好像只有一行一样。BorderCanvasActualHeightTextBlockActualHeight

文本块:

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

应用此样式,其中包含画布:

<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 背后的代码有:

public override void OnApplyTemplate()
{
    base.OnApplyTemplate();
    _contentPart = GetTemplateChild("PART_Content") as FrameworkElement;                        
}

然后只使用一个简单的 DoubleAnimation 我将它移动到屏幕上:

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();
}
4

2 回答 2

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();
}
于 2013-05-22T06:37:10.240 回答
0

我不确定这是否适用于您,但对我来说,Windows.UI.Xaml.Controls需要在文本块前面加上:

myTextBlock.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));

之前,当我有 时myTextBlock.Measure(new Size());,它在没有任何文本换行但有换行ActualWidthActualHeight返回单词/字母的尺寸时起作用,具体取决于 WrapWholeWords 或 Wrap

于 2014-01-14T23:16:59.740 回答