views:

297

answers:

3

I wanted to write a Custom Control that would show a family tree... so i looked on FamilyShow....

So their control inherits FrameworkElement but then every thing gets super complex... are there any easy tutorials out there that show how to implement a custom FrameworkElement with children and so on?

Basically what i fail to do is this, Add child controls and show them, and when drawing getting the coordinates of the child controls...

+1  A: 

I'd recommend looking at using a HierarchicalDataTemplate. Typically, there is a way to just use the built in controls, with a hierarchical data template, instead of generating a custom control.

Given your desire to show a family tree, it should be possible to do this directly in the standard WPF controls.

Reed Copsey
Yes i did look into using a tree control, but i never managed to get lines between the "parents" and the "child" any way i think it would be a good lesson to learn how to write custom controls..
Petoj
+1  A: 

A fully expanded TreeView control can be used to show a family tree. Josh Smith have some articles how to change its layout to what is commonly used for a family tree which you can adapt to your needs: http://www.codeproject.com/KB/WPF/CustomTreeViewLayout.aspx

If you still want to learn how to develop custom controls, pick something easier for you first custom controls than a family tree control.

Wallstreet Programmer
Well sure ill pick some thing easy but i still would need the information on placing my child elements getting their size and placment afterward..
Petoj
+1  A: 

What you are looking for is a Panel: It already exposes a Children property of type UIElementCollection, so all you need to do is add the children and override two methods:

MeasureOverride computes the desired size of your panel. You can return whatever size you like. To take all the available space, just return the constraint:

protected virtual Size MeasureOverride(Size availableSize)
{
  return availableSize;
}

ArrangeOverride computes the location of each child as a Rect. You can easily use attached properties to store additional data for each child. This can be publicly visible data such as DockPanel.Dock or Canvas.Top, or it can be private data you use to remember where everything goes and why. The skeleton for an ArrangeOverride is:

protected virtual Size ArrangeOverride(Size finalSize)
{
  foreach(UIElement child in Children)
  {
    Rect childLocation = ... code to compute child location ...
    child.Arrange(childLocation);
  }
  return finalSize;
}

For drawing lines, you can either use child controls or simply override OnRender and draw lines directly into the DrawingContext. OnRender is always called after ArrangeOverride is complete and has access to the actual locations of the children.

For detailed tutorials I would Bing "WPF Panel Tutorial" or "WPF Custom Panel Tutorial". Here's one that looked good.

Ray Burns
thanks alot!! if i could i would give you a +5!
Petoj