gpt4 book ai didi

c# - 如何设置 Canvas.ZIndex 在控件之间绘制黑色面板?

转载 作者:太空宇宙 更新时间:2023-11-03 14:11:19 24 4
gpt4 key购买 nike

通过以下代码,我可以演示如何在每个矩形顶部放置一个不透明度为 50% 的黑色面板:

 <Grid>
<Rectangle Fill="Black" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Opacity="0.5" Canvas.ZIndex="1"/>
<Rectangle Fill="Red" Width="200" Height="200" Canvas.ZIndex="0"/>
<Grid>
<Rectangle Fill="Blue" Width="100" Height="100" Canvas.ZIndex="0"/>
<Rectangle Fill="Yellow" Width="50" Height="50" Canvas.ZIndex="1"/>
</Grid>
</Grid>

看起来像这样:

enter image description here

我希望黑色面板上方有黄色矩形,但这似乎是不可能的。

我可以通过将包含蓝色和黄色矩形的网格的 ZIndex 设置为“1”来实现接近的效果。但这也会将蓝色矩形升高到黑色上方,这是一个问题。

<Grid>
<Rectangle Fill="Black" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Opacity="0.5" Canvas.ZIndex="1"/>
<Rectangle Fill="Red" Width="200" Height="200" Canvas.ZIndex="0"/>
<Grid Canvas.ZIndex="1">
<Rectangle Fill="Blue" Width="100" Height="100" Canvas.ZIndex="0"/>
<Rectangle Fill="Yellow" Width="50" Height="50" Canvas.ZIndex="1"/>
</Grid>
</Grid>

enter image description here

如何只得到黑色上方的黄色矩形?在我的实际应用程序中,我有用户控件而不是矩形。我喜欢通过将其他所有内容都覆盖在半黑色阴影中来使特定控件脱颖而出。

非常感谢,

最佳答案

我认为您无法通过当前的控件安排实现这一目标。

这里有两个级别的控件,内部网格内的“蓝色”和“黄色”控件,然后是与内部网格一起的“黑色”和“红色”控件。

ZIndex 在相同“级别”的控件上工作 - 因此您可以确保黄色控件位于蓝色之上,但在更高级别,这些控件被分组到内部网格下所以被视为一个整体。

唯一可行的方法是所有控件都处于同一级别。如果您在内部网格中包含第二个半不透明矩形,您可以使黄色位于那个之上,但这最终可能会使其他控件变得太暗。

关于c# - 如何设置 Canvas.ZIndex 在控件之间绘制黑色面板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7780459/

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