yozawiratama
yozawiratama

Reputation: 4328

wpf C# change layer position

I have XML code like this:

<rectangle />
<textbox />
<button Name="PositionChanger" />

If the button with name PositionChanger is clicked, the rectangle should move in front of the TextBox and the TextBox behind the rectangle.

Please help me to create it programically using C#.

Upvotes: 0

Views: 2411

Answers (1)

Haris Hasan
Haris Hasan

Reputation: 30117

You can use Zindex property to achieve this.

//initially text box would be on top because Zindex is set to 1 and Rectangle would be behind it
<TextBox x:Name="text" Panel.ZIndex="1" />
<Rectangle x:Name="rect" Panel.ZIndex="0"/>

Inside button click event do this

  Panel.SetZIndex(text, 0);
  Panel.SetZIndex(rect,1);

now rectangle would be on top and textbox would be behind rectangle

Upvotes: 3

Related Questions