开发者

WPF Writing custom Control

开发者 https://www.devze.com 2022-12-27 06:23 出处:网络
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 e

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 th开发者_开发百科em, and when drawing getting the coordinates of the child controls...


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号