【问题标题】:WPF Margin ThicknessWPF 边距厚度
【发布时间】:2011-11-06 17:35:39
【问题描述】:

背景: 我正在将图像加载到水平显示的堆栈面板(称为 MainStack)中(为了论证,10 个图像),只有 4 个图像的空间。当我从列表中加载图像时,我将每个图像的宽度设置为 300,因此它们都在相同大小的框中。

我想使用堆栈面板的边距(左)属性将图像从右向左移动。我希望向左滚动的外观与每个图像的宽度完全相同(循环延迟 4 秒),直到最后一个图像出现。这是我的 Margin 动画代码:

    Dim result As New Storyboard
    Dim animation As New ThicknessAnimation
    animation.From = MainStack.Margin
    animation.EasingFunction = New PowerEase() With {.EasingMode = EasingMode.EaseInOut, .Power = 3}
    animation.To = New Thickness(-300, 0, 0, 0)
    animation.Duration = New Duration(TimeSpan.FromSeconds(1.5))

    Storyboard.SetTarget(animation, MainStack)
    Storyboard.SetTargetProperty(animation, New PropertyPath("Margin"))
    result.Children.Add(animation)
    result.Begin()

奇怪的事情正在发生。堆栈面板向左移动,但仅移动了图像宽度的大约一半。

这是怎么回事?!?

/* 编辑 */ 根据 H.B.建议,我尝试实现 TranslateTransform 但没有太大成功。

谁能看出这段代码有什么问题?

    Dim translatePosition = New Point(300, 0)

    RenderTransform = New TranslateTransform()
    Dim d As New Duration(New TimeSpan(0, 0, 0, 1, 30))
    Dim x As New DoubleAnimation(translatePosition.X, d)

    Storyboard.SetTarget(x, MainStack)
    Storyboard.SetTargetProperty(x, New PropertyPath("(UIElement.RenderTransform).(TranslateTransform.X)"))

    Dim sb As New Storyboard()
    sb.Children.Add(x)
    sb.Begin()

似乎什么都没有发生。 本

【问题讨论】:

  • 为什么不应用 TranslateTransform 并为其设置动画?
  • 嗨 H.B.感谢您的评论,我正在研究它。你能建议我可以使用的任何代码吗?我正在阅读 TranslateTransform 不适合 Storyboard。
  • 这对我来说是新闻,谁说的?
  • 与它不是 UIElement 有关

标签: wpf animation width margin stackpanel


【解决方案1】:

我认为您应该尝试将整个堆栈面板放在画布中,并仅对 Canvas.Left 属性设置动画以滚动图像。

【讨论】:

    【解决方案2】:

    您的另一个选择是使用水平列表框,然后您可以为 ScrollViewer 设置动画。如果您想以这种方式尝试,这里的链接可能会有所帮助:WPF - Animate ListBox.ScrollViewer.HorizontalOffset?

    【讨论】:

      猜你喜欢
      • 2011-11-09
      • 2019-04-20
      • 2016-11-12
      • 1970-01-01
      • 2013-11-26
      • 1970-01-01
      • 1970-01-01
      • 2013-03-16
      • 1970-01-01
      相关资源
      最近更新 更多