自定义窗口拖动处理程序会干扰scrollviewer栏拖动

我有一个窗口样式设置为“无”的窗口,可以处理OnPreviewMouseDown,OnPreviewMouseUp和OnmouseMove,以便可以从任何位置拖动该窗口。

后面的Windows代码如下:

    bool inDrag;        

    protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
    {
        base.OnPreviewMouseLeftButtonDown(e);            
        inDrag = true;
    }

    protected override void OnmouseMove(MouseEventArgs e)
    {
        base.OnmouseMove(e);
        if (inDrag && e.LeftButton == MouseButtonState.pressed)
        {
            this.DragMove();
        }
    }

    protected override void OnPreviewMouseLeftButtonUp(MouseButtonEventArgs e)
    {
        base.OnPreviewMouseLeftButtonUp(e);
        if (inDrag)
        {
            inDrag = false;
        }
    }

现在的问题是,我在此窗口中有一个大菜单,其中包含一个滚动条,该滚动条可与滚动轮一起使用,并且通过单击滚动查看器上的某个位置来工作,但单击并拖动滚动条本身时却不能。同样,如果我单击并按住并将光标移到窗口上方,则滚动将再次起作用。这使我相信上述拖动实现阻止了scrollviewers的拖动功能。

我尝试在OnmouseMove中使用.RaiseEvent(e)手动引发事件,但是当我将鼠标移到窗口上方时,这会导致堆栈溢出异常。

如何在不删除单击和拖动窗口行为的情况下使Scrollviewer响应鼠标的移动?

bill58702738 回答:自定义窗口拖动处理程序会干扰scrollviewer栏拖动

<!--ScrollViewer subscribed to PreviewMouseDown to set inDrag to false-->
    <ScrollViewer Visibility="Visible" Mouse.PreviewMouseDown="ScrollViewer_PreviewMouseDown">
        <!--The Grid fill all the available space and is subscribed to PreviewMouseDown event to set inDrag to true-->
        <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Mouse.PreviewMouseDown="Grid_PreviewMouseDown" Background="Transparent">
            <!--My menu-->
        </Grid>
    </ScrollViewer>

请注意,如果存在“空白”(未设置背景(包括内容的背景)), 尽管已订阅,但不会引发PreviewMouseDown事件。 如果需要解决此问题,可以将“网格背景”设置为“透明”,即使在看似空白的地方也会引发该事件。

本文链接:https://www.f2er.com/2307747.html

大家都在问