如何为 WPF UserControl 编写 BringToFront 方法?

发布于 2024-09-12 02:04:22 字数 507 浏览 4 评论 0原文

我正在开发一个 UserControl,称之为 CoolControl,它的作用有点像一个窗口,具有一些特殊功能。到目前为止,它可以调整大小并在屏幕上拖动。如果我使用 XAML 将多个 CoolControl 对象添加到应用程序窗口,则声明的最后一个对象始终位于前面。这很好,但我想要这样做,以便如果我在运行时单击我的 CoolControl 对象之一,该控件将把自己放在所有其他控件的前面。

我尝试过使用 Canvas.SetZIndex ,但除非我根本无法想出一个足够聪明的解决方案,否则我看不出这对我有什么帮助。因为一旦我将一个控件的 Z-Index 设置为 9999,随着时间的推移,我单击的所有其他控件都将具有相同的值 9999。然后,最后声明的控件再次出现在前面。

如果您接到为某人的 UserControl 编写 BringToFront() 方法的任务,您将如何以最简单的方式完成它?我更喜欢一个更好的解决方案,而不是获取父窗口,循环遍历所有控件,找到最大 Z 索引,然后相应地设置 CoolControl 的 Z 索引(如果这甚至是有效的解决方案)。

I am developing a UserControl, call it CoolControl, that is meant to act somewhat like a window, with a few special features. So far, it can be resized and dragged all around the screen. If I add multiple CoolControl objects to my application window using XAML, the last one that was declared is always in front. This is fine, but I want to make it so that if I click on one of my CoolControl objects during run-time, that control will put itself in front of all the other controls.

I've tried using Canvas.SetZIndex, but unless I'm simply unable to come up with a clever enough solution, I don't see how that can help me. Because once I set one control's Z-Index to 9999, over time every other control I click will have the same value of 9999. And then, once again, the control declared last ends up in front.

If you were given the task of writing a BringToFront() method for someone's UserControl, how would you do it in the simplest way possible? I'd prefer a better solution than getting the parent window, looping through all the controls, finding the maximum Z-Index, and then setting the Z-Index of the CoolControl accordingly, if THAT is even a valid solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

无声静候 2024-09-19 02:04:22

我不熟悉 Canvas.SetZIndex 方法。它看起来像是某种附加属性或行为。

如果您可以提供设置 z 索引的逻辑,我已经概述了一种跟踪实例和管理 z 索引的方法,使它们保持选择/创建的顺序。

public class CoolControl : UserControl
{

    public CoolControl()
    {
        InitializeComponent();
        Instances.Add(this);
    }

    static IList<CoolControl> Instances = new List<CoolControl>();

    void SelectThisInstance()
    {
        foreach(var instance in Instances)
        {
            // decrement z-index instance
        }

        // set z-index of this instance to show at top
    }
}

I'm not familiar with the Canvas.SetZIndex method. It looks like some sort of attached property or behaviour.

If you can provide the logic to set the z-index, I've outlined a way to keep track of the instances and manage the z-indexes, keeping them in the order in which they have been selected/created.

public class CoolControl : UserControl
{

    public CoolControl()
    {
        InitializeComponent();
        Instances.Add(this);
    }

    static IList<CoolControl> Instances = new List<CoolControl>();

    void SelectThisInstance()
    {
        foreach(var instance in Instances)
        {
            // decrement z-index instance
        }

        // set z-index of this instance to show at top
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文