在另一个 FrameworkElement 中绘制 FrameworkElement
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过实现
LogicalChildren
方法?当我开发第一个“FrameworkElement”时,我遇到了各种各样的麻烦。
经过广泛的研究,我基本上发现您必须重写许多方法,以便它能够与
WPF
很好地配合。Have you tried implementing the
LogicalChildren
method?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
.至少您需要在 VisualCue 中重写 ArrangeOverride:
传递给 Indicator 子项的 Rect 中的值取决于您的布局需求。您还应该覆盖 MeasureOverride。
FrameworkElement.ArrangeOverride 方法
FrameworkElement.MeasureOverride 方法
At the very least you need to override ArrangeOverride in VisualCue:
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