如何解决如何控制网格的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,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”个孩子的排序。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。