简体   繁体   中英

WPF Writing custom Control

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...

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.

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.

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 .

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