2011-05-23 19 views
0

我需要在執行時用C#代碼中的對象執行「上移」,「下移」等對象,是否有任何可能性?在WPF中動態更改對象的圖層

+0

你可以進一步解釋你的解釋嗎? – 2011-05-23 05:09:37

+0

哦,對不起,沒有問題...例如,我有兩個控件在我的GRID上,它重疊...我需要將底部控件從C#代碼移動到前端...好嗎? – 2011-05-23 05:27:00

回答

2

你可以試試這個代碼:

private bool _isUp = false; 

private void button1_Click(object sender, RoutedEventArgs e) { 
    if (_isUp) { 
     Canvas.SetZIndex(rectangle1, 1); 
    } else { 
     Canvas.SetZIndex(rectangle1, 0); 
    } 

    _isUp = !_isUp; 
} 

我只是用2個矩形我的樣品中,這一點。

<Rectangle Height="100" HorizontalAlignment="Left" Margin="68,142,0,0" Name="rectangle1" Stroke="Black" VerticalAlignment="Top" Width="200" Fill="#FF9D2A2A" /> 
    <Rectangle Height="100" HorizontalAlignment="Left" Margin="10,120,0,0" Name="rectangle2" Stroke="Black" VerticalAlignment="Top" Width="200" Fill="#FF265D80" /> 
+0

好的,太棒了!佩頓,謝謝! – 2011-05-23 05:49:05

+0

謝謝,祝你好運。 – 2011-05-23 06:00:16