WPF OnRender Z-Order控件?

时间:2011-09-07 23:01:33

标签: wpf panel z-order onrender

我有一个自定义面板,可以实现其子项的自定义布局。我想在面板上添加一些额外的图形,因此重写OnRender以添加额外的矩形等。除了OnRender似乎只添加到背景之外,它的效果很好。有没有办法让你的OnRender绘图在孩子们之上?

示例小组......

public class RenderPanel : WrapPanel
{
    protected override void OnRender(DrawingContext drawingContext)
    {
        base.OnRender(drawingContext);
        drawingContext.DrawRectangle(Brushes.Red, 
                                     new Pen(Brushes.Black, 2), 
                                     new Rect(0, 0, 100, 100));
    }
}

......以这种方式使用......

<Grid>
    <my:RenderPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <Button Margin="10" Content="Hello"/>
        <Button Margin="10" Content="Hello"/>
        <Button Margin="10" Content="Hello"/>
        <Button Margin="10" Content="Hello"/>
    </my:RenderPanel>
</Grid>

1 个答案:

答案 0 :(得分:1)

我会让你的“RenderPanel”继承自canvas,然后将它放在WrapPanel之上并根据需要指定ZIndex。这样的事情应该有效:

<Grid>
    <WrapPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Canvas.ZIndex="0">
        <Button Margin="10" Content="Hello"/>
        <Button Margin="10" Content="Hello"/>
        <Button Margin="10" Content="Hello"/>
        <Button Margin="10" Content="Hello"/>
    </WrapPanel>
    <my:RenderPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Canvas.ZIndex="1" />
</Grid>