如何控制网格的 Z 顺序

发布于 2024-11-17 04:43:44 字数 1469 浏览 2 评论 0原文

我有以下代码:

        TextBlock tmp = new TextBlock
        {
            Text = displayText,
            Foreground = new SolidColorBrush(Colors.Red),
            HorizontalAlignment = HorizontalAlignment.Center,
            VerticalAlignment = VerticalAlignment.Center,
            FontSize = 30
        };
        Grid grd = new Grid();
        grd.Children.Add(tmp);
        // grd.Background = new SolidColorBrush(Colors.LightGray);
        Viewbox vb = new Viewbox();
        vb.Child = grd;
        vb.Width = width;
        vb.Height = height;
        DrawingCvs.Children.Add(vb);
        Canvas.SetLeft(vb, xpos);
        Canvas.SetTop(vb, ypos);
        Canvas.SetZIndex(grd, 1000);

        // we need a second grid for cosmetic reasons. Due to
        // how the viewbox works, when we resize we lose the full
        // width of the grid which has a textblock in it
        Grid cosmeticGrd = new Grid
                          {
                              Width = width,
                              Height = height,
                              Background = new SolidColorBrush(Colors.LightGray)
                          };
        DrawingCvs.Children.Add(cosmeticGrd);
        Canvas.SetLeft(cosmeticGrd, xpos);
        Canvas.SetTop(cosmeticGrd, ypos);
        Canvas.SetZIndex(grd, 0);

我想要的是代码中首先添加的网格位于第二个添加的网格之上。我的 Z-Index 属性设置有什么问题吗?或者是别的什么?

InB4 更改在代码中添加它们的顺序 - 是的,我意识到这一点,但作为理解的一点,我想了解 ZIndex 属性。

I have the following code:

        TextBlock tmp = new TextBlock
        {
            Text = displayText,
            Foreground = new SolidColorBrush(Colors.Red),
            HorizontalAlignment = HorizontalAlignment.Center,
            VerticalAlignment = VerticalAlignment.Center,
            FontSize = 30
        };
        Grid grd = new Grid();
        grd.Children.Add(tmp);
        // grd.Background = new SolidColorBrush(Colors.LightGray);
        Viewbox vb = new Viewbox();
        vb.Child = grd;
        vb.Width = width;
        vb.Height = height;
        DrawingCvs.Children.Add(vb);
        Canvas.SetLeft(vb, xpos);
        Canvas.SetTop(vb, ypos);
        Canvas.SetZIndex(grd, 1000);

        // we need a second grid for cosmetic reasons. Due to
        // how the viewbox works, when we resize we lose the full
        // width of the grid which has a textblock in it
        Grid cosmeticGrd = new Grid
                          {
                              Width = width,
                              Height = height,
                              Background = new SolidColorBrush(Colors.LightGray)
                          };
        DrawingCvs.Children.Add(cosmeticGrd);
        Canvas.SetLeft(cosmeticGrd, xpos);
        Canvas.SetTop(cosmeticGrd, ypos);
        Canvas.SetZIndex(grd, 0);

What I want is for the Grid added first in the code to be above the grid added second. What is wrong with my Z-Index property setting here? Or is it something else?

InB4 Change the order you add them in the code--yes I realize this, but as a point of understanding I want to understand the ZIndex property.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梓梦 2024-11-24 04:43:44

您的第一个 SetZindex 调用:

Canvas.SetZIndex(grd, 1000);

错误地将设置应用于 Grid 而不是 Viewbox。它应该是:

Canvas.SetZIndex(vb, 1000);

因为 ViewboxCanvas 的子级。

同样,您的第二个 SetZIndex 调用:

Canvas.SetZIndex(grd, 0);

应用于错误的 Grid。它应该是:

Canvas.SetZIndex(cosmeticGrd, 0);

总之,Canvas.ZIndex 附加属性会影响 Canvas 子项的顺序。

Your first SetZindex call:

Canvas.SetZIndex(grd, 1000);

incorrectly applies the setting to the Grid instead of the Viewbox. It should be:

Canvas.SetZIndex(vb, 1000);

because the Viewbox is the child of the Canvas.

Likewise, your second SetZIndex call:

Canvas.SetZIndex(grd, 0);

is applied to the wrong Grid. It should be:

Canvas.SetZIndex(cosmeticGrd, 0);

In summary, the Canvas.ZIndex attached property affects the ordering of Canvas children.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文