views:

770

answers:

2

I can't wrap my head around how to accomplish rendering this

public class Shape{}
public class Circle: Shape{}
public class Square: Shape
{
    public List<Circle> CircleList{ get; private set; }
}

I have a List that holds Shape objects, now what I want to accomplish is having each object rendered in a grid.

If the object is a Square there should be a nested grid that holds Circle items from the CircleList property

I've tried with an ItemsControl and a HierarchicalDataTemplate, could not get it working, i've tried nesting an ItemsControl inside an ItemsControl, i'm pretty new to WPF so i'm kinda fumbling around here not knowing what the "proper" solution would be. I did manage to render the above in a TreeView, but what i'm trying to accomplish is a drawingboard that renders shapes.

UPDATE

The "Drawingboard" should contain items, each item should be rendered in a container.

If the object is of Type Square the Square container should have a nested container to hold the Circle objects from the CircleList Property.

A: 

You could try using two DataTemplates, one for a Circle (just renders a Circle) and one for a Square. The Square DataTemplate should render a Grid (just give it a border to make it look like a square) and then set the nested Grid's DataContext="{Binding CircleList}".

I'm not 100% sure how you're converting a list of shapes to a grid, but sounds like you've already got that solved, so I'll just omit that for simplicity. :)

Scott Whitlock
i'm kinda confused as to what I would be defining the specific datatemplates in, basicly if I knew how to make it, i'd use an ItemsControl, I want to have a "drawingboard" this drawingboard holds a list of items, theese items should be rendered in a certain manner.
thmsn
@thmsn - The "drawingboard" you're describing is a bit odd, from a functionality standpoint. It won't allow items to overlap with each other. Why not just use a Canvas object, throw all your shapes in there, and let it render them. Then you don't have to worry about what's "inside" of what.
Scott Whitlock
the reason I want it to be nested is because the child objects position needs to be relative to the parents position :) they should allow to overlap though, so a canvas would propably be the right thing, they still need a nsted canvas though :)
thmsn
@thmsn - consider structuring the tree backwards. Add a Parent property to Shape (of type Shape). Have just one list of all shapes, and add them all to your Canvas. Have each shape dynamically calculate its own properties off of its parent's properties. Have each shape subscribe to the property changed event of its parent too, so they recalculate on the fly, and raise their own property changed events.
Scott Whitlock
+1  A: 
Charlie
+1 - I was a little confused by the original question saying he was displaying them in a grid, and I figured he had some other data template for rendering a "grid" from a list. You've solved that with an ItemsControl.
Scott Whitlock
nice, atleast I got it to render now :D now I got something to work with :)
thmsn
You're welcome. You could accept my answer, too. :)
Charlie
Going too, just got so busy when I finally got it working :D
thmsn