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?
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
.
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
精彩评论