gpt4 book ai didi

WPF OnRender Z 顺序控制?

转载 作者:行者123 更新时间:2023-12-03 03:47:54 29 4
gpt4 key购买 nike

我有一个自定义面板,它实现了其子项的良好自定义布局。我想向面板添加一些额外的图形,因此覆盖 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>

最佳答案

我会让你的“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>

关于WPF OnRender Z 顺序控制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7341450/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com