如何在 wpf 中将项目带到前面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/453613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I bring an item to the front in wpf?
提问by Jonathan Beerhalter
I simply have two grid on top of one another. Given one state of the world, I want grid A to be on top, given another state of the world, I want grid B to be on top. In the old days we could just call grid.BringToFront(), but that doesn't exist anymore, and I can't figure out any way to make that happen.
我只是有两个网格在彼此之上。给定世界的一种状态,我希望网格 A 位于顶部,考虑到世界的另一种状态,我希望网格 B 位于顶部。在过去,我们可以只调用 grid.BringToFront(),但现在已经不存在了,我想不出任何方法来实现它。
The best I can figure, I need to create my own custom classes to allow this functionality, but that seems like major overkill for something that used to be so simple.
尽我所能,我需要创建自己的自定义类来实现此功能,但这对于过去如此简单的东西来说似乎是一种大材小用。
回答by Ifeanyi Echeruo
You can use the Panel.ZIndexproperty to change the display order of elements in a panel
您可以使用Panel.ZIndex属性来更改面板中元素的显示顺序
回答by Pic Mickael
You have to use the Z index property, and because there are no built-in function to do what you want, I made my own. The higher the Z value, the 'closer' to front the control is. So you want to put your control on top without having to set an arbitrary high Z value.
你必须使用 Z 索引属性,因为没有内置函数来做你想做的,我自己做了。Z 值越高,控件越靠前。所以你想把你的控制放在最上面,而不必设置任意的高 Z 值。
So here is a small function I wrote for myself to do exactly that. Note: this assume that you are using a Canvas and UserControls. So you might need to adapt it a little bit if that's not your case.
所以这是我为自己编写的一个小函数来做到这一点。注意:这里假设您使用的是 Canvas 和 UserControls。因此,如果不是您的情况,您可能需要对其进行一些调整。
Basically it will get the index of the control to move, then any control currently above it will go down by 1 and the control to move will be put on top (to maintain hierarchy).
基本上它会得到要移动的控件的索引,然后当前在它上面的任何控件都将下降 1,而要移动的控件将放在顶部(以保持层次结构)。
static public void BringToFront(Canvas pParent, UserControl pToMove)
{
try
{
int currentIndex = Canvas.GetZIndex(pToMove);
int zIndex = 0;
int maxZ = 0;
UserControl child;
for (int i = 0; i < pParent.Children.Count; i++)
{
if (pParent.Children[i] is UserControl &&
pParent.Children[i] != pToMove)
{
child = pParent.Children[i] as UserControl;
zIndex = Canvas.GetZIndex(child);
maxZ = Math.Max(maxZ, zIndex);
if (zIndex > currentIndex)
{
Canvas.SetZIndex(child, zIndex - 1);
}
}
}
Canvas.SetZIndex(pToMove, maxZ);
}
catch (Exception ex)
{
}
}
回答by Adam Calvet Bohl
To whom it may concern:
敬启者:
ZIndex
property is 0 by default, so if you have (like me) a Canvas with more than 1 element (>4000 Shapes in my case), all will have ZIndex = 0
, so changing the ZIndexes with this method will have no effect.
ZIndex
属性默认为 0,所以如果你(像我一样)有一个超过 1 个元素的画布(在我的例子中为 >4000 个形状),所有元素都会有ZIndex = 0
,所以使用此方法更改 ZIndexes 将不起作用。
For this to work, I set the ZIndexes to a known value after creating all the elements, so they can be ordered after.
为此,我在创建所有元素后将 ZIndexes 设置为已知值,以便可以在之后对它们进行排序。
int zIndex = 0;
for (int i = 0; i < canvas.Children.Count; i++) {
UIElement child = canvas.Children[i] as UIElement;
if (canvas.Children[i] is UIElement) Canvas.SetZIndex(child, zIndex++);
}
回答by Jonas H. Anderson
Instead of stacking the two grids, change the visibility properties so the grid you aren't using is collapsed.
不是堆叠两个网格,而是更改可见性属性,以便折叠您不使用的网格。