在WPF中动态更改对象的图层

时间:2011-05-23 04:28:48

标签: wpf visual-studio-2010 layer

执行时,我需要做一些类似“向上移动”,“向下移动”的CID代码中我的GRID上的对象,是否有可能?

1 个答案:

答案 0 :(得分: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" />
相关问题