在 TreeView 中添加/删除项目的动画 - WPF

发布于 2024-11-08 02:24:49 字数 758 浏览 0 评论 0原文

我知道有人提出了与 ItemsControl 中的动画项目相关的类似问题,因此如果已发布可接受的答案,那么如果我能指出正确的方向,那就太好了。

我有一个数据绑定 TreeView,其中层次结构只有一层深。它可以有 X 个根节点和 X 个子节点。然而,这已经是最深的了。我面临的问题是如何以动画方式在数据绑定集合中添加/移动/删除项目。

TreeView 的 ItemContainerStyle 设置为自定义 ControlTemplate,以提供 MouseOver 和 Selected 样式。然后,我使用 HierarchialDataTemplate 进一步布局绑定到它的不同类型的 Item。

我发现了许多示例,演示了如何对项目的扩展进行动画处理,但在添加/移动/删除项目时的动画方面却没有很多。 此处找到了一个示例 几乎显示了我想要的内容,但包含了大量代码和库引用,换句话说,我认为这些代码和库引用是无关的。我并不反对合并我自己的类似功能版本。我只是希望我可以连接到事件触发器或等效项来实现相同的目标。

我没有发布任何示例代码,因为我真的对任何解决方案持开放态度,并且没有编写任何“不起作用”且需要修复的内容。我或多或少正在寻找关于从哪里开始的意见。所应用的动画类型也无关紧要,因为只要我知道如何为添加/移动/删除设置动画,那么我就可以修改和更改代码以适应我的喜好和整体最终效果。

I know similar questions have been asked in relation to animating items within an ItemsControl so if an acceptable answer has been posted then it would be great if I could be pointed in the right direction.

I have a databound TreeView where the hierarchy will only ever be one level deep. It could have X number of root nodes with X number of children. However that's as deep as it gets. The problem I'm facing is how is the best way to animate the add/move/remove of items in the databound collection.

The TreeView's ItemContainerStyle is set to a custom ControlTemplate, to provide MouseOver and Selected styling. Then I use a HierarchialDataTemplate to further layout the different types of Item's being bound to it.

I have found many examples demonstrating how to animate the Expanding of items but not a whole lot in terms of animating when an item is added / moved / removed. One example found here
shows pretty much what I'm after but incorporates a lot of code and library references that I would in other words consider extraneous. I'm not against incorporating my own versions of similar functionality. I'm just hoping I can hook into event triggers or equivalent to accomplish the same goal.

I haven't posted any example code because I'm really open to any solution and haven't written anything that "does't work" and needs fixing. I am more or less looking for opinions on where to start. The type of animation being applied is also irrelevant because as long as I know how to animate the add/move/remove at all, then I can modify and change the code to suit my preferences and overall final effects.

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

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

发布评论

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

评论(1

鲸落 2024-11-15 02:24:49

因为我认为您在问“我应该如何解决这个问题?”我可以给你一些建议。

您手头有两个基本问题:

  • 您试图实现的效果机制
  • ,将该效果连接到您的程序架构中

。对于第一个问题,我建议使用简单的非 MVVM 应用程序对您的效果进行原型设计。特别是,您可以使用您引用的链接中的 VisualStateManager 想法和您已经熟悉的 Animation 概念以及良好的老式代码隐藏,无需复杂的库来获取树插入、移动和移除效果起作用。请记住,稍后您将尝试摆脱所有隐藏代码,并且这仅用于原型设计。基本上,您将该链接移植到代码隐藏并删除它使用的任何内容。

一旦你获得了你想要达到的效果,现在你就遇到了古老的 MVVM 问题,即让它以“松散耦合”的方式与你的视图模型一起工作。 WPF 使数据绑定属性变得简单,因此 MVVM 就是数据领域的 MVVM。但对于操作来说,将事件与操作干净地连接起来有很多很多障碍,而且每个 MVVM 框架似乎都以不同的方式做到这一点。 MVVMLight 有 EventToCommandSystem.Windows.InteractivityCallMethodAction 并且它会一直持续下去。

因此,要将您的效果与您选择的 MVVM 框架集成,请使用该框架的机制将事件连接到操作(无论是什么)。但是,如果您找不到其他方法来使粘合发挥作用,您总是可以使用一些隐藏代码来处理视图或将事件直接分派到视图模型。由你决定。

Since I think you are asking "how should I approach this problem?" I can give you some suggestions.

You have two basic problems at hand:

  • the mechanics of the effect you are trying to achieve
  • wiring that effect into your program's architecture

For the first problem I would recommend prototyping your effects using a simple non-MVVM application. In particular you can use the VisualStateManager ideas from the link you referenced and Animation concepts you are already familiar with and good old fashioned code-behind with no complicated libraries to get the tree insertion, motion, and removal effects working. Remember that you are going to try to get rid of all the code-behind later and that this is only for prototyping. Basically you are porting that link to code-behind and removing whatever cruft it uses.

Once you get the effect you are trying to achieve, now you have the age old MVVM problem of making it work with your view-model with "loose coupling". WPF makes databinding properties straightforward so MVVM is MVVM all the world around for data. But for operations there are many, many hurdles to connecting events to operations cleanly and every MVVM framework seems to do it differently. MVVMLight has EventToCommand and System.Windows.Interactivity has CallMethodAction and it goes on and on.

So to integrate your effect with your MVVM framework of choice, use that framework's mechanism to connect events to operations, whatever that is. However, you can always use a little bit of code-behind to work with your view or dispatch events directly to your view model if you can't find another way to make the glue work. It's up to you.

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