2011-06-23 23 views
0

我有下面的代碼的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

你的第一個電話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孩子的順序。

+0

啊,啊!有時候只需要另一雙眼睛,謝謝! – themaestro