自定义窗口拖动处理程序会干扰滚动查看器栏拖动

问题描述 投票:0回答:1

我有一个窗口样式设置为无的窗口,它处理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响应鼠标的移动而不删除我的单击和拖动窗口行为?

wpf window scrollviewer
1个回答
0
投票
<!--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事件。如果需要解决此问题,可以将“网格背景”设置为“透明”,即使在看似空白的地方也会引发该事件。

© www.soinside.com 2019 - 2024. All rights reserved.