自定义 window 拖动处理程序会干扰滚动查看器栏拖动
Custom window drag handler interferes with scrollviewer bar drag
我有一个 window,window 样式设置为 None,它处理 OnPreviewMouseDown、OnPreviewMouseUp 和 OnMouseMove,以便可以从任何地方拖动 window。
后面的 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;
}
}
现在的问题是我在这个 window 中有一个大菜单,它有一个与滚轮一起工作的滚动条,通过单击滚动查看器上的某个位置但不是在单击并拖动滚动条本身时.此外,如果我单击并按住并将光标移动到不在 window 上方,则滚动会再次起作用。这让我相信上面的拖动实现阻止了 scrollviewers 拖动功能。
我尝试在 OnMouseMove 中使用 .RaiseEvent(e) 手动引发事件,但是当我将鼠标移到 window.
上时,这会导致堆栈溢出异常
如何让我的 Scrollviewer 响应鼠标移动而不删除我的点击和拖动 window 行为?
<!--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>
请注意,如果有 "blank space"(没有设置包括内容在内的背景),
尽管订阅, PreviewMouseDown 事件不会被引发。
如果需要,要解决此问题,您可以将网格的背景设置为透明,即使看似空白 space.
也会引发事件
我有一个 window,window 样式设置为 None,它处理 OnPreviewMouseDown、OnPreviewMouseUp 和 OnMouseMove,以便可以从任何地方拖动 window。
后面的 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;
}
}
现在的问题是我在这个 window 中有一个大菜单,它有一个与滚轮一起工作的滚动条,通过单击滚动查看器上的某个位置但不是在单击并拖动滚动条本身时.此外,如果我单击并按住并将光标移动到不在 window 上方,则滚动会再次起作用。这让我相信上面的拖动实现阻止了 scrollviewers 拖动功能。
我尝试在 OnMouseMove 中使用 .RaiseEvent(e) 手动引发事件,但是当我将鼠标移到 window.
上时,这会导致堆栈溢出异常如何让我的 Scrollviewer 响应鼠标移动而不删除我的点击和拖动 window 行为?
<!--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>
请注意,如果有 "blank space"(没有设置包括内容在内的背景), 尽管订阅, PreviewMouseDown 事件不会被引发。 如果需要,要解决此问题,您可以将网格的背景设置为透明,即使看似空白 space.
也会引发事件