如何为 WPF UserControl 编写 BringToFront 方法?
我正在开发一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不熟悉 Canvas.SetZIndex 方法。它看起来像是某种附加属性或行为。
如果您可以提供设置 z 索引的逻辑,我已经概述了一种跟踪实例和管理 z 索引的方法,使它们保持选择/创建的顺序。
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.