BringToFront / SetZIndex在通用Windows应用程序

本文关键字:Windows 应用程序 SetZIndex BringToFront | 更新日期: 2023-09-27 18:17:25

显然Grid.SetZIndex()不存在。ctrl.SetValue(Grid.ZIndexProperty…).

那么我如何把Grid的孩子带到前面呢?

BringToFront / SetZIndex在通用Windows应用程序

ZIndex仅在Canvas控件内有效。

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

否则,例如在grid中,文档大纲中的位置决定z索引。也就是说,它在XAML页面中的位置越晚,它在Z索引中的位置就越高。

祝你好运!

在这种情况下,你可以在Children中找到你的元素并将其移动到顶部:

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

这个例子写了"在膝盖上"但我经常用这个小技巧。或者您可以尝试创建这样的扩展名:

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

但是要注意父元素的类型,或者不要使用泛型