13

I have three buttons lying one by one. I want to show top of all button, which i click. So that I have set canvas ZIndex in XAML Code. But I want to do this in Code Behind.

My XAML Output & Code -

image

<Button Name="Button1" Canvas.ZIndex="3" Content="Button1" Canvas.Top="100" Width="163" Height="58" FontSize="26" Click="Button1_Click" />
<Button Name="Button2" Canvas.ZIndex="2" Content="Button1" Canvas.Top="100" Canvas.Left="130" Width="163" Height="58" FontSize="26" Click="Button2_Click" />
<Button Name="Button3" Canvas.ZIndex="1" Content="Button1" Canvas.Top="100" Canvas.Left="260" Width="163" Height="58" FontSize="26" Click="Button3_Click" />

My XAML Output & Code (Check How the ZIndex was Changed)-

image

<Button Name="Button1" Canvas.ZIndex="3" Content="Button1" Canvas.Top="100" Width="163" Height="58" FontSize="26" Click="Button1_Click" />
<Button Name="Button2" Canvas.ZIndex="2" Content="Button1" Canvas.Top="100" Canvas.Left="130" Width="163" Height="58" FontSize="26" Click="Button2_Click" />
<Button Name="Button3" Canvas.ZIndex="1" Content="Button1" Canvas.Top="100" Canvas.Left="260" Width="163" Height="58" FontSize="26" Click="Button3_Click" />

How I change the Canvas.ZIndex property in WPF button control in Click Event?

slavoo
  • 5,798
  • 64
  • 37
  • 39
Sagotharan
  • 2,586
  • 16
  • 73
  • 117

3 Answers3

32

You can set z index in code like this:

Panel.SetZIndex(control name, int index);
Niels R.
  • 7,260
  • 5
  • 32
  • 44
A.K.
  • 3,321
  • 1
  • 15
  • 27
7

An attached property is set from code using by convention the SetPropertyName function.

Panel.SetZIndex(Button2, 0);
Novitchi S
  • 3,711
  • 1
  • 31
  • 44
  • 2
    I prefer the Panel method, as that is where it is declared. And ReSharper warns of accessing a static member via a derived class. – Jørn Jensen Jul 01 '14 at 07:46
1

Z-index canbe set like this..

 Panel.SetZIndex(Button2, 10);
Clemens
  • 123,504
  • 12
  • 155
  • 268
Sandeep Chauhan
  • 1,313
  • 1
  • 10
  • 23