简体   繁体   中英

Dragable shapes in WPF with childs

I've a problem with my shapes application. I want a app which can make schema's out of XML. My problem is the drag option for the parents. So i can have a ellipse with two rectangles in it for example. When I drag the parent, it has to drag the childs as well.

How I set it up:

<contentcontrol>
   <itemscontrol>
      <ellipse>Parent</ellipse>
   </itemscontrol>
   <itemscontrol>
      <rectangle>Child1</rectangle>
   </itemscontrol>
   <itemscontrol>
      <rectangle>Child2</rectangle>
   </itemscontrol>
</contentcontrol>

I think it's not possible to have more objects at the contentcontrol. My contentcontrol is allowed to drag and drop, and when it's like this, it's also allowed to drag and drop the childs with it

BUT, the problem with this, the positioning doesn't fit when I just draw it on the canvas. So with the itemscontrol my whole idea with the positioning is screwed. What is a good alternative of a solution to this problem?

Can somebody help? Thanks a lot!

Remember, ContentControl is itself a Control , allowing you to nest them:

Compiled in VS2013 C#

<ContentControl>
    <ItemsControl>
        <!-- One shape in the top level parent. -->
        <Ellipse Width="100" Height="100" Stroke="#FFDE3737" />
        <ContentControl>
            <ItemsControl>
                <!-- Multiple shapes in one sub-parent -->
                <Ellipse Width="100" Height="100" Stroke="#FFDE3737" />
                <Ellipse Width="100" Height="100" Stroke="#FFDE3737" />
            </ItemsControl>
        </ContentControl>
    </ItemsControl>
</ContentControl>

EDIT: A useful tip to know when working with WPF composition. Many Controls derive from the same base Control . In that manner, they form a kind of composite pattern allowing you to nest them very deeply when necessary.

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