如何将 wpf 中的项目置于最前面?

发布于 2024-07-11 07:18:15 字数 201 浏览 5 评论 0原文

我只是将两个网格叠在一起。 给定世界的一种状态,我希望网格 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

哽咽笑 2024-07-18 07:18:15

您可以使用 Panel.ZIndex属性来更改面板中元素的显示顺序

You can use the Panel.ZIndex property to change the display order of elements in a panel

守望孤独 2024-07-18 07:18:15

您必须使用 Z 索引属性,并且因为没有内置函数来执行您想要的操作,所以我制作了自己的函数。
Z 值越高,控件越“靠近”前端。
因此,您希望将控件放在顶部,而不必设置任意高的 Z 值。

所以这是我为自己编写的一个小函数,就是为了做到这一点。
注意:这假设您正在使用 Canvas 和 UserControls。
因此,如果这不是您的情况,您可能需要稍微调整一下。

基本上,它将获取要移动的控件的索引,然后当前位于其上方的任何控件都会减 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)
            {
            }
    }

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).

    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)
            {
            }
    }
醉南桥 2024-07-18 07:18:15

可能涉及的人:

ZIndex 属性默认为 0,因此如果您(像我一样)有一个包含超过 1 个元素的 Canvas(在我的情况下为 > 4000 个形状) ),所有的都将具有 ZIndex = 0,因此使用此方法更改 ZIndex 不会产生任何效果。

为此,我在创建所有元素后将 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++);
    }

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.

For this to work, I set the ZIndexes to a known value after creating all the elements, so they can be ordered after.

    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++);
    }
美人如玉 2024-07-18 07:18:15

不要堆叠两个网格,而是更改可见性属性,以便折叠未使用的网格。

Instead of stacking the two grids, change the visibility properties so the grid you aren't using is collapsed.

oО清风挽发oО 2024-07-18 07:18:15

扩展来自 @PicMickael答案,这将完全按照他们所描述的方式进行,但指令较少:

public void BringToFront<T>(T uiElement, Canvas canvas)
{
    try
    {
        foreach (UIElement s in canvas.Children)
        {
            Canvas.SetZIndex(s, 1);
        }

        Canvas.SetZIndex(uiElement as UIElement, 2);
    }
    catch (Exception ex)
    {
        WriteLog.Error(ex);
    }
}

Expanding on the answer from @PicMickael, this will do exactly as they described but with less instructions:

public void BringToFront<T>(T uiElement, Canvas canvas)
{
    try
    {
        foreach (UIElement s in canvas.Children)
        {
            Canvas.SetZIndex(s, 1);
        }

        Canvas.SetZIndex(uiElement as UIElement, 2);
    }
    catch (Exception ex)
    {
        WriteLog.Error(ex);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文