在另一个 FrameworkElement 中绘制 FrameworkElement

发布于 2024-08-27 11:03:52 字数 932 浏览 9 评论 0原文

public class VisualCue : FrameworkElement
{
    public List<Indicator> Indicators { get; set; }

    public VisualCue()
    {
        this.Indicators = new List<Indicator>();
    }

    protected override int VisualChildrenCount
    {
        get { return this.Indicators.Count; }
    }

    protected override Visual GetVisualChild(int index)
    {
        return this.Indicators[index];
    }
}

public class Indicator : FrameworkElement
{
    protected override void OnRender(DrawingContext context)
    {
        context.DrawEllipse(Brushes.Red, 
            new Pen(Brushes.Black, 2), new Point(0, 0), 10, 10);

        base.OnRender(context);
    }
}

在 XAML 中:

<local:VisualCue x:Name="visualCue">
 <local:VisualCue.Indicators>
  <local:Indicator />
 </local:VisualCue.Indicators>
</local:VisualCue>

但是指示器没有被绘制。我缺少什么?

public class VisualCue : FrameworkElement
{
    public List<Indicator> Indicators { get; set; }

    public VisualCue()
    {
        this.Indicators = new List<Indicator>();
    }

    protected override int VisualChildrenCount
    {
        get { return this.Indicators.Count; }
    }

    protected override Visual GetVisualChild(int index)
    {
        return this.Indicators[index];
    }
}

public class Indicator : FrameworkElement
{
    protected override void OnRender(DrawingContext context)
    {
        context.DrawEllipse(Brushes.Red, 
            new Pen(Brushes.Black, 2), new Point(0, 0), 10, 10);

        base.OnRender(context);
    }
}

And in XAML:

<local:VisualCue x:Name="visualCue">
 <local:VisualCue.Indicators>
  <local:Indicator />
 </local:VisualCue.Indicators>
</local:VisualCue>

But the indicator doesn't get drawn. What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟花易冷人易散 2024-09-03 11:03:52

您是否尝试过实现 LogicalChildren 方法?

protected override IEnumerator LogicalChildren
{
    get { return indicators.GetEnumerator(); }
}

当我开发第一个“FrameworkElement”时,我遇到了各种各样的麻烦。

经过广泛的研究,我基本上发现您必须重写许多方法,以便它能够与WPF很好地配合。

Have you tried implementing the LogicalChildren method?

protected override IEnumerator LogicalChildren
{
    get { return indicators.GetEnumerator(); }
}

I had all sorts of trouble when I developed my first 'FrameworkElement`.

After extensive research I basically discovered there were a number of methods you have to override in order for it to play nicely with WPF.

往事风中埋 2024-09-03 11:03:52

至少您需要在 VisualCue 中重写 ArrangeOverride:

protected override Size ArrangeOverride(Size finalSize)
{
    foreach (Indicator indicator in Indicators)
    {
        indicator.Arrange(new Rect(0, 0, 10, 10));
    }

    return finalSize;
}

传递给 Indicator 子项的 Rect 中的值取决于您的布局需求。您还应该覆盖 MeasureOverride。

FrameworkElement.ArrangeOverride 方法

FrameworkElement.MeasureOverride 方法

At the very least you need to override ArrangeOverride in VisualCue:

protected override Size ArrangeOverride(Size finalSize)
{
    foreach (Indicator indicator in Indicators)
    {
        indicator.Arrange(new Rect(0, 0, 10, 10));
    }

    return finalSize;
}

The values in the Rect passed to Indicator children depends on your layout needs. You should also override MeasureOverride as well.

FrameworkElement.ArrangeOverride method

FrameworkElement.MeasureOverride method

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文