如何创建包含(并显示)其他UIElements作为子项的自定义UIElement派生类?

d7s*_*rai 3 c# wpf rendering custom-controls uielement

假设我想创建一个直接继承的类,UIElement并且能够包含一个或多个[外部添加] UIElement作为子项 - 比如Panels和其他容器控件.很容易让班级UIElement以某种形式或其他形式收集s,但我如何让它们与我的班级一起展示/呈现?

我认为必须将它们添加到可视化树为我自己的孩子,UIElement以某种方式(或可能的话,手动使他们通过LA去VisualTreeHelper.GetDrawing,并用做OnRenderDrawingContext?但似乎笨拙).

不是想知道我可以-或者应该-从多个现成的控制,继承一样FrameworkElement,Panel,ContentControl等(如果有的话,我想知道如何,他们正在实施外部添加子元素的显示/渲染,如适用) .

我有理由希望在层次结构中尽可能高,所以请不要给我任何关于为什么XAML/WPF框架"合规"等是好事的讲座等.

Cle*_*ens 7

以下类在子元素的布局和呈现方面提供绝对最小值:

public class UIElementContainer : UIElement
{
    private readonly UIElementCollection children;

    public UIElementContainer()
    {
        children = new UIElementCollection(this, null);
    }

    public void AddChild(UIElement element)
    {
        children.Add(element);
    }

    public void RemoveChild(UIElement element)
    {
        children.Remove(element);
    }

    protected override int VisualChildrenCount
    {
        get { return children.Count; }
    }

    protected override Visual GetVisualChild(int index)
    {
        return children[index];
    }

    protected override Size MeasureCore(Size availableSize)
    {
        foreach (UIElement element in children)
        {
            element.Measure(availableSize);
        }

        return new Size();
    }

    protected override void ArrangeCore(Rect finalRect)
    {
        foreach (UIElement element in children)
        {
            element.Arrange(finalRect);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

不需要具有UIElementCollection.另一种实现可能如下所示:

public class UIElementContainer : UIElement
{
    private readonly List<UIElement> children = new List<UIElement>();

    public void AddChild(UIElement element)
    {
        children.Add(element);
        AddVisualChild(element);
    }

    public void RemoveChild(UIElement element)
    {
        if (children.Remove(element))
        {
            RemoveVisualChild(element);
        }
    }

    // plus the four overrides
}
Run Code Online (Sandbox Code Playgroud)