WPF 边距厚度

发布于 2024-12-02 05:44:34 字数 1365 浏览 0 评论 0原文

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

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

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

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

到底是怎么回事?!?

/* 编辑 */ 根据 HB 的建议,我尝试实现 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()

似乎什么也没有发生。 本

Background:
I'm loading images into a Stack Panel (called MainStack) displayed horizontally (for argument sake, 10 images) with only room for 4 images in view. As I load the images from the List I'm setting the width of each to 300 so they're all in the same size box.

I want to move the images from right to left using the Stack Panel's Margin (left) property. I want the appearance of scrolling left by exactly the same amount of the width of each image (looped with 4 second delay) until the last image is in view. Here's my code for the Margin animation:

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

Strange thing is happening. The Stack Panel is moving to the left but only by about half the width of the image.

What is going on?!?

/* edit */
As per H.B. suggestion, I've tried to implement a TranslateTransform but not having much success.

Can anyone see any problems with this code?

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

Nothing seems to be happening.
Ben

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

⊕婉儿 2024-12-09 05:44:34

我认为您应该尝试将整个堆栈面板放入画布中,然后仅对 Canvas.Left 属性进行动画处理以滚动图像。

I think you should try putting your entire stackpanel in a canvas and just animating the Canvas.Left property to scroll the images.

另类 2024-12-09 05:44:34

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

Another option for you is to use a horizontal ListBox, then you can animate the ScrollViewer. If you want to try it this way, here's a link that may help: WPF - Animate ListBox.ScrollViewer.HorizontalOffset?.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文