如何控制网格的Z顺序

| 我有以下代码:
        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属性。     
已邀请:
        您的第一个“ѭ1”通话:
Canvas.SetZIndex(grd, 1000);
错误地将设置应用到
Grid
而不是
Viewbox
。它应该是:
Canvas.SetZIndex(vb, 1000);
因为
Viewbox
Canvas
的子代 同样,您的第二个
SetZIndex
跟注:
Canvas.SetZIndex(grd, 0);
应用于错误的
Grid
。它应该是:
Canvas.SetZIndex(cosmeticGrd, 0);
总之,附加的“ 12”个属性会影响“ 7”个孩子的排序。     

要回复问题请先登录注册