多层“全部展开”对于扩展器?

发布于 2024-12-14 12:55:49 字数 587 浏览 3 评论 0原文

我正在编写一个 MVVM 配置接口,并且我正在尝试实现一个适用于所有视图的展开/折叠所有功能。

顶级 ViewModel 是其他 ViewModel 的同类集合,它绑定到视图中的 ItemsControl。这些子 ViewModel 中的每一个都有许多依赖的 ViewModel,每个 ViewModel 都绑定到 View 中的 ContentControl。从视觉上看,这采用一组扩展器(例如 3 个)的形式,每个扩展器内部隐藏有 4-6 个扩展器。

我尝试过自上而下的搜索方法。由于顶级扩展器已折叠,因此它们的子扩展器在第一次显示之前甚至不存在(Caliburn.Micro 在第一次要求它们时为它们执行查看位置),并且“全部展开”行动仅扩展第一级。

逻辑树也没有任何帮助。顶级 ItemsControl 的 Items 属性实际上包含我的 ViewModel 类型的对象,因此我无法在它们内部查找扩展器。

我曾考虑过在 ViewModel 级别进行管理,但向所有虚拟机添加 IsExpanded 属性并使用某些全局管理器进行设置似乎很混乱。虚拟机没有其他原因知道哪些扩展器已扩展。

处理这个问题的最佳方法是什么?我可以使用一些附加属性绑定技巧吗?

I'm writing an MVVM configuration interface, and I'm trying to implement an expand/collapse all function that applies to all the views.

The top-level ViewModel is a homogeneous collection of other ViewModels, which is bound to an ItemsControl in the view. Each of these child ViewModels has a number of dependent ViewModels, each of which is bound to a ContentControl in the View. Visually, this takes the form a a set of expanders (say, 3 of them), each of which has 4-6 expanders hidden inside.

I've tried top-down search methods. Since the top-level expanders are collapsed, their child expanders don't even exist until they are shown for the first time (Caliburn.Micro is doing view location for them when they're first asked for), and the "expand all" action only expands the first level.

The logical tree isn't any help either. The top-level ItemsControl's Items property actually contains objects of my ViewModel type, so I can't look inside them for expanders.

I've thought of managing this at the ViewModel level, but adding an IsExpanded property to all of the VMs and setting it using some global manager seems messy. There's no other reason for the VM to know which expanders are expanded.

What's the best way of handling this? Is there some attached-property binding trick I could pull?

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

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

发布评论

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

评论(1

梅倚清风 2024-12-21 12:55:49

您的 ViewModel 应该有一个可以扩展自身并在其子级中调用相同函数的函数。

public abstact class BaseItem
{
   public List<BaseItem> Children;

   public virtual void SetSelfAndChildrenExpandedState(bool inState)
   {
      // Expand Self
      IsExpanded = inState;
      // Expand Children
      foreach (BaseItem i in Children)
      {
          i.SetSelfAndChildrenExpandedState(inState);
      }
   }

   public bool IsExpanded { get; set; }
}

现在您只需连接“全部展开”操作即可使用适当的参数调用 SetSelfAndChildrenExpandedState() 。

这可能对你有用......考虑一下。

Your ViewModel should have a function that expands itself and calls the same function in its children.

public abstact class BaseItem
{
   public List<BaseItem> Children;

   public virtual void SetSelfAndChildrenExpandedState(bool inState)
   {
      // Expand Self
      IsExpanded = inState;
      // Expand Children
      foreach (BaseItem i in Children)
      {
          i.SetSelfAndChildrenExpandedState(inState);
      }
   }

   public bool IsExpanded { get; set; }
}

Now you just have to hook up your "expand all" action to call SetSelfAndChildrenExpandedState() with the appropriate parameter.

That might work for you...give it a thought.

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