如何将 wpf 中的项目置于最前面?
我只是将两个网格叠在一起。 给定世界的一种状态,我希望网格 A 位于顶部,给定世界的另一种状态,我希望网格 B 位于顶部。 在过去,我们可以只调用 grid.BringToFront() ,但现在已经不存在了,我想不出任何方法来实现这一点。
我能想到的最好的办法是,我需要创建自己的自定义类来允许此功能,但这对于过去如此简单的东西来说似乎是一个重大的矫枉过正。
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用 Panel.ZIndex属性来更改面板中元素的显示顺序
You can use the Panel.ZIndex property to change the display order of elements in a panel
您必须使用 Z 索引属性,并且因为没有内置函数来执行您想要的操作,所以我制作了自己的函数。
Z 值越高,控件越“靠近”前端。
因此,您希望将控件放在顶部,而不必设置任意高的 Z 值。
所以这是我为自己编写的一个小函数,就是为了做到这一点。
注意:这假设您正在使用 Canvas 和 UserControls。
因此,如果这不是您的情况,您可能需要稍微调整一下。
基本上,它将获取要移动的控件的索引,然后当前位于其上方的任何控件都会减 1,并且要移动的控件将被放在顶部(以保持层次结构)。
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.
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.
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).
可能涉及的人:
ZIndex
属性默认为 0,因此如果您(像我一样)有一个包含超过 1 个元素的 Canvas(在我的情况下为 > 4000 个形状) ),所有的都将具有ZIndex = 0
,因此使用此方法更改 ZIndex 不会产生任何效果。为此,我在创建所有元素后将 ZIndexes 设置为已知值,以便之后可以对它们进行排序。
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 haveZIndex = 0
, so changing the ZIndexes with this method will have no effect.For this to work, I set the ZIndexes to a known value after creating all the elements, so they can be ordered after.
不要堆叠两个网格,而是更改可见性属性,以便折叠未使用的网格。
Instead of stacking the two grids, change the visibility properties so the grid you aren't using is collapsed.
扩展来自 @PicMickael 的答案,这将完全按照他们所描述的方式进行,但指令较少:
Expanding on the answer from @PicMickael, this will do exactly as they described but with less instructions: