简体   繁体   English

拖动时更改 ZIndex

[英]Changing ZIndex on drag

I'm dragging around different StackPanel s inside a canvas but when I drag over another StackPanel inside the same canvas, that is overlapping it grabs that one instead of the one I'm currently dragging.我在画布内拖动不同的StackPanel但是当我在同一画布内拖动另一个StackPanel ,重叠它会抓住那个而不是我当前拖动的那个。 I am trying to solve this with Zindex .我正在尝试用Zindex解决这个Zindex Is this the correct way?这是正确的方法吗? My code so far:到目前为止我的代码:

private void UIElement_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    StackPanel sp = (StackPanel)sender;
    Panel.SetZIndex(sp, 99999);
    sp.Background = Brushes.Red;
    Console.WriteLine(Panel.GetZIndex(sp));
}

private void UIElement_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    StackPanel sp = (StackPanel)sender;
    Panel.SetZIndex(sp, -10);
    sp.Background = Brushes.Green;
    Console.WriteLine(Panel.GetZIndex(sp));
}

private void UIElement_OnMouseMove(object sender, MouseEventArgs e) 
{
    if (e.LeftButton == MouseButtonState.Pressed) 
    { 
        StackPanel sp = (StackPanel) sender; 
        SymbolModel sm = sp.DataContext as SymbolModel; 
        Point pos = e.GetPosition(SymbolViewControl); 
        if (pos.X > 10 && pos.Y > 10) 
        { 
            sm.CanvasTop = pos.Y-10; 
            sm.CanvasLeft = pos.X-10; 
        } 
    } 
}

The colors and the writeline are only for testing and they seem to work as intended.颜色和 writeline 仅用于测试,它们似乎按预期工作。 The Zindex gets set correctly on button down and button up but the elements seem to not care. Zindex 在按下按钮和按下按钮时正确设置,但元素似乎并不关心。

Using CaptureMouse and ReleaseMouseCapture inside the mousedown and mouseup methods seems to have fixed the main issue.在 mousedown 和 mouseup 方法中使用 CaptureMouse 和 ReleaseMouseCapture 似乎已经解决了主要问题。

private void UIElement_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        StackPanel sp = (StackPanel)sender;
        Panel.SetZIndex(sp, 99999);
        sp.CaptureMouse(); //This
    }

    private void UIElement_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        StackPanel sp = (StackPanel)sender;
        Panel.SetZIndex(sp, -10);
        sp.ReleaseMouseCapture(); //And this
    } 

It new holds onto the one i started grapping no matter what unltil release.无论什么直到发布,它都会保持我开始抓紧的那个。 The Z-index is still wrong and i still need a fix for that. Z-index 仍然是错误的,我仍然需要修复它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM