【问题标题】:Added Thumb not rendered添加的拇指未呈现
【发布时间】:2016-02-03 12:07:41
【问题描述】:

我想实现我自己的从 FrameworkElement 派生的控件,但未呈现添加的子元素。

我不知道为什么。

public class RangeSelection : FrameworkElement
{
    private Thumb thumb = null;

    #region Construction / Destruction

    public RangeSelection()
    {
        this.thumb = new Thumb();
        this.thumb.Width    = 32.0;
        this.thumb.Height   = 32.0;
        this.AddVisualChild(this.thumb);

    }

    #endregion

    protected override Size MeasureOverride(Size availableSize)
    {
        this.thumb.Measure(availableSize);
        return new Size(64.0, 64.0);
    }

    protected override Size ArrangeOverride(Size finalSize)
    {
        this.thumb.Arrange(new Rect(0, 0, 64.0, 64.0));
        return base.ArrangeOverride(finalSize);
    }
}

【问题讨论】:

    标签: c# wpf frameworkelement


    【解决方案1】:

    您需要覆盖VisualChildrenCount 属性和GetVisualChild 方法。像这样的:

    protected override int VisualChildrenCount
    {
        get { return thumb == null ? 0 : 1; }
    }
    
    protected override Visual GetVisualChild(int index)
    {
        if (_child == null)
        {
            throw new ArgumentOutOfRangeException();
        }
    
        return _child;
    }
    

    如果您想要更多的子元素,您应该使用某种集合来存储子元素,然后您将返回集合的计数或适当的集合元素。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-17
      • 2012-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多