简体   繁体   English

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

[英]How to set Canvas.ZIndex to paint a black panel between the controls?

With the following code I can demonstrate how a black panel with a opacity of 50% is on top of every rectangle: 通过以下代码,我可以演示不透明度为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>

It looks like this: 看起来像这样:

在此处输入图片说明

I would like to have the yellow rectangle above the black panel, but that seems to be impossible. 我想在黑色面板上方有一个黄色矩形,但这似乎是不可能的。

I can achieve something close by setting the ZIndex of the Grid containing both the Blue and Yellow rectangles to "1". 我可以通过将同时包含蓝色和黄色矩形的Grid的ZIndex设置为“ 1”来实现一些目标。 But this would also raise the blue rectangle above the black, and this is a problem. 但这也会使蓝色矩形高于黑色,这是一个问题。

<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>

在此处输入图片说明

How do I get only the yellow rectangle above the black? 如何仅获得黑色上方的黄色矩形? In my real application I have user controls instead of the rectangles. 在我的实际应用程序中,我有用户控件而不是矩形。 I like to make a particular control standing out by having everything else covered by the half-black shade. 我喜欢通过将其他所有内容都覆盖在半黑色阴影中来突出显示特定控件。

Many Thanks, 非常感谢,

I don't think you'll be able to achieve this with your current arrangement of controls. 我认为您无法通过当前的控件排列来实现这一目标。

There are two levels of controls here, the "Blue" and "Yellow" controls inside the inner grid and then the "Black" and "Red controls together with the inner grid. 这里有两个级别的控件,内部网格内部的“蓝色”和“黄色”控件,然后是内部网格的“黑色”和“红色”控件。

The ZIndex works on controls at the same "level" - so you can ensure that the yellow control is on top of the blue, but then at the higher level these are grouped under the inner grid so are treated as a single unit. ZIndex在相同“级别”上的控件上工作-因此,您可以确保黄色控件位于蓝色的顶部,但是在较高级别上,它们被分组在内部网格下,因此被视为一个单元。

The only way this would work is if all your controls were at the same level. 唯一可行的方法是所有控件都处于同一级别。 If you included a second semi opaque rectangle in the inner grid you could get the yellow to be on top of that but that might end up making other controls too dark. 如果在内部网格中包含第二个半不透明矩形,则可能会使黄色位于其顶部但这可能最终会使其他控件变得太暗。

One approach might be to not use just a simple black rectangle. 一种方法可能是不只使用简单的黑色矩形。

Instead use a Path composed of two rectangles. 而是使用由两个矩形组成的路径。 The first rectangle will cover the whole area and the second would just cover the control to be available. 第一个矩形将覆盖整个区域,第二个矩形将仅覆盖可用的控件。

This creates a large rectangle with a hole in it where your target control can show through and accept input. 这将创建一个较大的矩形,其中带有孔,目标控件可以在其中显示并接受输入。

The down side is working out the rectangle geometry to add to create the hole but that's fairly straight forward. 缺点是正在计算要添加以创建孔的矩形几何形状,但这相当简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM