将控件设置为最顶层的 silverlight

发布于 2024-11-08 18:16:26 字数 1152 浏览 6 评论 0原文

[情况]

我有一个自定义项目控件,我将其添加到堆栈面板中,然后可以通过拖动自由移动。

public partial class CustomItem: UserControl
{
    private bool IsDragging { get; set; }
    private Point clickPosition;
    public CustomItem()
    {
        InitializeComponent();
        this.DataContext = this;
        this.MouseLeftButtonDown += (s, ea) =>
        {

            clickPosition = ea.GetPosition(this.LayoutRoot);
            this.CaptureMouse();
            IsDragging = true;
        };
        this.MouseMove += (s, ea) =>
        {
            if (IsDragging)
            {                    
                this.transFormThisShit.X = ea.GetPosition(this).X - clickPosition.X;
                this.transFormThisShit.Y = ea.GetPosition(this).Y - clickPosition.Y;
            }
        };
        this.MouseLeftButtonUp += (s, ea) =>
        {
            this.ReleaseMouseCapture();
            IsDragging = false;
        };                      
    }

[问题]

添加 3 个项目后,我将第一个项目拖到第二个或第三个项目上,它会跳到它后面。

当我将第二个项目拖到第一个项目上时,它会在前面,但是当我将它拖到第三个项目上时,它会在后面。

如何才能使控件拖动始终位于可视化树的顶部?

[Situation]

I have a custom item control which I add to a stack panel and after that is freely to move around by dragging.

public partial class CustomItem: UserControl
{
    private bool IsDragging { get; set; }
    private Point clickPosition;
    public CustomItem()
    {
        InitializeComponent();
        this.DataContext = this;
        this.MouseLeftButtonDown += (s, ea) =>
        {

            clickPosition = ea.GetPosition(this.LayoutRoot);
            this.CaptureMouse();
            IsDragging = true;
        };
        this.MouseMove += (s, ea) =>
        {
            if (IsDragging)
            {                    
                this.transFormThisShit.X = ea.GetPosition(this).X - clickPosition.X;
                this.transFormThisShit.Y = ea.GetPosition(this).Y - clickPosition.Y;
            }
        };
        this.MouseLeftButtonUp += (s, ea) =>
        {
            this.ReleaseMouseCapture();
            IsDragging = false;
        };                      
    }

[Problem]

After I add 3 items, and I drag the first item over the second or third, it jumps behind it.

When I drag the second item over the first, it goes in front but when I drag it over the third it goes behind it.

How can I make it so the control dragging is always on top of the visual tree?

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

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

发布评论

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

评论(2

你曾走过我的故事 2024-11-15 18:16:26

您可以设置 ZIndex 值高于其他项目。数字越高,项目距离屏幕越“近”,因此最小的数字将位于底部。

You can set the ZIndex value of the item being dragged to be higher than the others. The higher the number, the 'closer' the item is to the screen, therefore the lowest number will be at the bottom.

笑忘罢 2024-11-15 18:16:26

当您开始拖动元素时,使用以下代码将其 Canvas.ZIndex 属性设置为大于 0 的值:

Canvas.SetZIndex(element,99);

并在完成拖动时清除它:

element.ClearValue(Canvas.ZIndex);

这样就可以了。

Use following code to set it's Canvas.ZIndex property to something greater than 0 when you start dragging your element:

Canvas.SetZIndex(element,99);

and clear it when you are done dragging:

element.ClearValue(Canvas.ZIndex);

That should do it.

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