如何在 XAML 中将父级的 ItemsHost 用于 HierarchicalDataTemplate?

发布于 2024-11-24 18:19:34 字数 1274 浏览 2 评论 0原文

我有一个树状或菜单状的数据结构,我想在 Panel 中显示它(特别是自制的 PolarPanel),但所有项目都必须是 VisualChildren 的相同的面板。

如果我定义一个 HierarchicalDataTemplate,我需要创建一个 ItemsControls 并将其指定为 IsItemsHost,还是我在这里弄错了?我可以以某种方式引用父项的 ItemsHost 吗?

    <HierarchicalDataTemplate
        DataType="{x:Type local:ItemsCollection}"
        ItemsSource="{Binding Children}"
        >
        <!--/* "virtual" ItemsHost referencing the parent's ItemsHost? */-->
    </HierarchicalDataTemplate>

(我的第一个解决方法是使用 GetAllTheChildren 属性在根节点中,以递归方式返回所有子节点和 SelectedChild 的所有子节点,这不容易与 ObservableCollection 一起使用,因为。更改必须冒泡到根才能再次刷新 GetAllTheChildren (这会重新加载比所需更多的项目)。)

我知道我可以通过创建和绘制items' Views 进入同一个父级,但我正在寻找一种很好的 WPF、间接、自动方式。

编辑:我现在使用改进的解决方法,其中所有 ItemControls / Panels 都绘制在彼此之上。这保留了层次结构,这使得它成为比上面提到的解决方案(使用 GetAllTheChildren)更好的解决方案。但它仍然没有回答我的问题...

这就是它的图形外观:
PolarMenu 示例
请注意分层元素如何排列在同一空间中。

有没有一个干净简单的解决方案?

I have a tree- or menu-like data structure which I want to display in a Panel (specifically a self-made PolarPanel), but all items must be VisualChildren of the same panel.

If I define a HierarchicalDataTemplate, I need to create an ItemsControls and specify it as IsItemsHost, or am I mistaken here? Can I somehow reference the parent's ItemsHost?

    <HierarchicalDataTemplate
        DataType="{x:Type local:ItemsCollection}"
        ItemsSource="{Binding Children}"
        >
        <!--/* "virtual" ItemsHost referencing the parent's ItemsHost? */-->
    </HierarchicalDataTemplate>

(My first workaround was to have a GetAllTheChildren property in the root node which returns all children and all children of the SelectedChild, recursively. This doesn't easily work together with ObservableCollection, because changes have to bubble up to the root to refresh GetAllTheChildren again (which reloads more items than would be neccessary).)

I know I can do it in code-behind by creating and drawing the items' Views into the same parent, but I'm searching for a nice WPF, indirect, automagic way.

EDIT: I am now using an improved workaround, where all ItemControls / Panels are drawn on top of each other. This preserves the hierarchical aspect, which makes this a much better solution than the one mentioned above (with GetAllTheChildren). But it still doesn't answer my question...

This is what it looks like graphically:
PolarMenu example

Note how the hierarchical elements have to be arranged in the same space.

Is there a clean and simple solution to this?

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

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

发布评论

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

评论(1

情归归情 2024-12-01 18:19:34

我必须承认,图形结果非常吸引人,即使我不知道如何使用这种控件!

为了回答你的问题,我从未尝试过与主持人共享同一面板,但我认为这是不可能的。这会破坏可视化树层次架构。任何视觉元素都不能有多个父元素。

假设每个层次级别都映射自己的环,我肯定会选择使用经典方法来创建这样的复合 UI。毫无疑问,使用 HierarchicalTemplates 和 ItemsControls 非常方便,即使它可能是一项复杂的任务。

此外,我不明白你的目标是否是有某种崩溃,比如普通的树视图。在这种情况下,避免经典的 WPF 方法将是一场噩梦!

我的解决方案是创建一个自定义面板,能够将他们的孩子安排在一个环上。

这还不是全部。必须同时创建自定义控件和 ItemsControl。第一个将代表必须渲染的环切片,另一个则作为生成器。
充当项目持有者的控件将具有两个属性:角度和半径。厚度可以分开设定。

调用 MeasureOverride 的面板必须考虑环嵌套,并相应地设置其子项的角度和半径。

从我的角度来看,持有控件应该派生自 ContentControl。这是因为您需要渲染切片的形状,并且该任务应该由该控件完成。任何特定的嵌入式控件(文本、按钮等)都可以作为内容放置在内部。

当然,这一切都是头脑风暴。我不能保证这是最好的解决方案,也不保证它会按原样工作。

希望有帮助。

干杯

I must admit that the graphic result is very attractive, even I wouldn't know how to use that kind of control!

To answer to your question, I never tried to share the same panel as host, but I don't think would be possible. That would break the visual tree hierarchy schema. Any visual element cannot have more than a parent.

Assuming that every hierarchy level maps his own ring, I surely bet on the classical way to create such a composite UI. There's no doubt about the convenience of using the HierarchicalTemplates and ItemsControls, even it could be a complex task.

Moreover, I don't understand whether your goal is to have some kind of collapse, such an ordinary tree-view. In this case, avoiding the classical WPF approach would be a nightmare!

My solution is to create a custom panel, being able to arrange their children on a ring.

That's not all. It must be created both a custom Control and an ItemsControl. The first one will represent the ring-slice that has to be rendered, and the other just as the generator.
The control acting as item holder will have two properties: angle and radius. The thickness can be set apart.

The panel, calling MeasureOverride, must take in account the ring nesting, and set both the angle and radius of their children accordingly.

From my viewpoint, the holding control should derive from a ContentControl. That because you need to render the shape of the slice, and that task should be done by this control. Any particular embedded control (text, buttons, etc) can be placed inside as content.

Of course, all that is brainstorming. I'm not guarantee that is the best solution, nor that would work as is.

Hope it helps.

Cheers

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