![](/img/trans.png)
[英]XAML GridViewColumn DisplayMemberBinding does not seem to work
[英]XAML Visual layer does not seem to be able to animate the Size target property
我有一個自定義面板,該面板使用可視層嘗試為每個孩子的Offset和Size屬性設置動畫,以便它們為新的位置和尺寸設置動畫。
它可以為Offset設置動畫效果,但完全忽略對Size設置動畫的任何嘗試。 甚至可以設置Size屬性的動畫嗎? 還是有一些特殊的東西可以阻止它的發生?
public class CustomPanel : Panel
{
private HashSet<UIElement> _elements = new HashSet<UIElement>();
protected override Size MeasureOverride(Size availableSize)
{
Size totalSize = new Size();
foreach (UIElement child in Children)
{
if (!_elements.Contains(child))
{
Visual visual = ElementCompositionPreview.GetElementVisual(child);
Compositor compositor = visual.Compositor;
ImplicitAnimationCollection implicits = compositor.CreateImplicitAnimationCollection();
Vector3KeyFrameAnimation offsetAnimation = compositor.CreateVector3KeyFrameAnimation();
offsetAnimation.Target = nameof(Visual.Offset);
offsetAnimation.InsertExpressionKeyFrame(1.0f, "this.FinalValue");
offsetAnimation.Duration = TimeSpan.FromSeconds(1);
Vector2KeyFrameAnimation sizeAnimation = compositor.CreateVector2KeyFrameAnimation();
sizeAnimation.Target = nameof(Visual.Size);
sizeAnimation.InsertExpressionKeyFrame(1.0f, "this.FinalValue");
sizeAnimation.Duration = TimeSpan.FromSeconds(1);
implicits[nameof(Visual.Offset)] = offsetAnimation;
implicits[nameof(Visual.Size)] = sizeAnimation;
visual.ImplicitAnimations = implicits;
_elements.Add(child);
}
child.Measure(availableSize);
totalSize.Width = Math.Max(totalSize.Width, child.DesiredSize.Width);
totalSize.Height += child.DesiredSize.Height;
}
return totalSize;
}
protected override Size ArrangeOverride(Size finalSize)
{
Point anchorPoint = new Point(0, 0);
foreach (UIElement child in Children)
{
child.Arrange(new Rect(anchorPoint, child.DesiredSize));
anchorPoint.Y += child.DesiredSize.Height;
anchorPoint.X += child.DesiredSize.Height / 2;
}
return finalSize;
}
}
也許您可以看看MSDN上的這篇文章 ,該文章解釋了如何為框架元素的大小設置動畫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.