简体   繁体   English

WPF编写自定义控件

[英]WPF Writing custom Control

I wanted to write a Custom Control that would show a family tree... so i looked on FamilyShow.... 我想写一个自定义控件来显示一棵家谱...所以我看了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? 因此,他们的控件继承了FrameworkElement,但是每件事都变得非常复杂...是否有任何简单的教程来展示如何使用子对象实现自定义FrameworkElement等等?

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 . 我建议看一下使用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. 考虑到您希望显示一棵家谱,应该可以直接在标准WPF控件中执行此操作。

A fully expanded TreeView control can be used to show a family tree. 完全展开的TreeView控件可用于显示家谱。 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 乔什·史密斯(Josh Smith)撰写了一些文章,介绍如何将其布局更改为可以适应您的需求的家谱常用的布局: 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: 您正在寻找的是Panel :它已经公开了UIElementCollection类型的Children属性,因此您所需要做的就是添加子项并覆盖两个方法:

MeasureOverride computes the desired size of your panel. MeasureOverride计算面板的所需大小。 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. ArrangeOverride将每个孩子的位置计算为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. 这可以是公共可见的数据,例如DockPanel.Dock或Canvas.Top,也可以是用于记住所有内容以及原因的私有数据。 The skeleton for an ArrangeOverride is: ArrangeOverride的框架是:

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并将线直接绘制到DrawingContext OnRender is always called after ArrangeOverride is complete and has access to the actual locations of the children. 总是在ArrangeOverride完成后调用OnRender并且可以访问子级的实际位置。

For detailed tutorials I would Bing "WPF Panel Tutorial" or "WPF Custom Panel Tutorial". 有关详细的教程,请使用“ WPF面板教程”或“ WPF自定义面板教程”。 Here's one that looked good . 看起来不错

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM