简体   繁体   中英

How do I use parent's ItemsHost for HierarchicalDataTemplate in XAML?

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 示例
Note how the hierarchical elements have to be arranged in the same space.

Is there a clean and simple solution to this?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM