0

I have a TextBlock which I move within a Canvas via DoubleAnimation(). On the enclosing Window SizeChanged event, I am able to properly resize the TextBlock.FontSize and inner Canvas, but I am having problems getting the position of the TextBlock correctly within the Canvas. (I was trying to do some form of Canvas.SetTop(NameQueueTextBlock, <newVal>) but that didn't work.)

<Canvas Grid.Column="1" ClipToBounds="True">
    <Canvas Name="NameQueueCanvas" ClipToBounds="True" Height="79" Width="309">
        <TextBlock Canvas.Top="0" Name="NameQueueTextBlock" FontSize="19" Text="&#10;"/>
    </Canvas>
</Canvas>
4

1 回答 1

1

我猜你DoubleAnimation是罪魁祸首。

如果它保持在根据 WPF 优先级系统Canvas.Top移动TextBlock任何未来更新的最终值(这是默认值)将“出现”被忽略。Canvas.Top

解决方案:

转变

Canvas.SetTop(NameQueueTextBlock, /*newVal*/);

NameQueueTextBlock.BeginAnimation(Canvas.TopProperty, null);
Canvas.SetTop(NameQueueTextBlock, /*newVal*/);    

你应该被分类。

替代方法:

假设你Storyboard被叫了sb,就在打电话之前sb.Begin();

添加类似的内容:

sb.Completed += (o, args) => {
  var finalVal = Canvas.GetTop(NameQueueTextBlock);
  NameQueueTextBlock.BeginAnimation(Canvas.TopProperty, null);
  Canvas.SetTop(NameQueueTextBlock, finalVal);
};

我更喜欢这个,因为它允许您不跟踪哪个代码片段可能会首先更改,Canvas.Top并预先使用动画TextBlock重置属性。null

于 2013-06-20T15:32:25.393 回答