[英]How to Control Z Order of Grids
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?
我的Z-Index属性设置在这里有什么问题? 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. InB4更改将它们添加到代码中的顺序-是的,我知道这一点,但是作为一个理解点,我想了解ZIndex属性。
Your first SetZindex
call: 您的第一个
SetZindex
调用:
Canvas.SetZIndex(grd, 1000);
incorrectly applies the setting to the Grid
instead of the Viewbox
. 错误地将设置应用于
Grid
而不是Viewbox
。 It should be: 它应该是:
Canvas.SetZIndex(vb, 1000);
because the Viewbox
is the child of the Canvas
. 因为
Viewbox
是Canvas
的子级。
Likewise, your second SetZIndex
call: 同样,您的第二个
SetZIndex
调用:
Canvas.SetZIndex(grd, 0);
is applied to the wrong Grid
. 应用于错误的
Grid
。 It should be: 它应该是:
Canvas.SetZIndex(cosmeticGrd, 0);
In summary, the Canvas.ZIndex
attached property affects the ordering of Canvas
children. 总之,
Canvas.ZIndex
附加属性会影响Canvas
子级的顺序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.