Button bt = ui as Button; Canvas parent = bt.Parent as Canvas; if (parent != null) { IEnumerable<UIElement> uiE = parent.Children.OfType<UIElement>().Where(x => x != ui);//枚举类型定义 if (uiE.Count()>0)//判断 除去用户选择的控件,是否还有其他控件。 { var maxZ = uiE.Select(x => Canvas.GetZIndex(x)).Max(); Canvas.SetZIndex(bt, maxZ + 1);//置于最顶层 } }
Button bt = ui as Button; if (bt.GetZIndex() == 0) { Panel.SetZIndex(bt, bt.GetZIndex()); } Panel.SetZIndex(bt, 0);//置于最底层
上面的方法,我只是举了一个列子,可以写成一个方法,后面调用。