3

Apparently Grid.SetZIndex() doesn't exist . As doesn't ctrl.SetValue(Grid.ZIndexProperty…).

So how do I bring a Child of Grid to Front?

Community
  • 1
  • 1
ispiro
  • 26,556
  • 38
  • 136
  • 291
  • By using the DOM correctly. Or if you must, canvas.zindex – Chris W. Oct 12 '15 at 20:32
  • Ah, fair enough, I just saw the *-store-apps tag, I retracted the close, but you could still use the DOM correctly and accomplish the same. – Chris W. Oct 12 '15 at 20:49
  • @ChrisW. Thanks. But how would I do that (in codebehind, of course)? – ispiro Oct 12 '15 at 20:52
  • It's all in how your elements get drawn, top to bottom is back to front. Personally I'm a big xaml fan so I'd rarely advocate invoking UI elements from codebehind. – Chris W. Oct 12 '15 at 21:02

2 Answers2

7

ZIndex is only valid inside a Canvas control.

<Canvas>
    <Grid Canvas.ZIndex="0"/>     
    <Grid Canvas.ZIndex="1"/>     
</Canvas>

Otherwise, such as in a grid, the placement in the document outline determines the z index. That is to say, the later it is in your XAML page the higher it is in the Z index.

Best of luck!

Jerry Nixon
  • 31,313
  • 14
  • 117
  • 233
  • Thanks. But a) According to [this answer](http://stackoverflow.com/a/573921/939213) it can be used for Grid also. b) So how would I add a Child in a certain location in the Grid's Children? – ispiro Oct 13 '15 at 10:27
0

In this case you can find your UIElement in Children and move it on top:

// Type of parent element could be any your control
public static void MoveUiOnTop(UIElement element, Grid yourParentElement)  
{
   int index = yourParentElement.Children.IndexOf(element);
   if (index != -1)
     yourParentElement.Children.Move((uint)index, (uint)yourParentElement.Children.Count - 1);
}

This example written "on the knee" but I use this small trick from time to time. Or you can try to create extension like this:

public static void MoveUiOnTop(this UIElement element, T yourParentElement)

but be careful with type of parent element or do not use generic.

Dmytro
  • 71
  • 7