Artem Makarov
Artem Makarov

Reputation: 874

Change object's layer dynamically in WPF

I need to do something like "Move up", "Move down" with the objects on my GRID from C# code while executing, is there any possibilities?

Upvotes: 0

Views: 439

Answers (1)

Peyton Crow
Peyton Crow

Reputation: 882

You can try this code:

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;
}

I just use 2 rectangles in my sample, for this.

    <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" />

Upvotes: 2

Related Questions