让WPF窗口拖动的,不管是什么点击元素 [英] Make WPF window draggable, no matter what element is clicked

查看:939
本文介绍了让WPF窗口拖动的,不管是什么点击元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是2倍,而我希望有双方所提供的解决方案更容易的 WPF ,而不是从的WinForms标准溶液(其中克里斯托夫格尔斯提供的,之前我做了这一澄清)

My question is 2 fold, and I am hoping there are easier solutions to both provided by WPF rather than the standard solutions from WinForms (which Christophe Geers provided, before I've made this clarification).

首先,有没有一种方法,使窗口拖动无捕获和处理鼠标点击+拖动事件?我的意思是窗口标题栏可拖动,但如果我设置一个窗口,不是有一个,仍然希望能够拖动它,有没有办法只有重新定向的事件不知何故到任何处理的标题栏拖动?

First, is there a way to make Window draggable without capturing and processing mouse-click+drag events? I mean the window is draggable by the title bar, but if I set a window not to have one and still want to be able to drag it, is there a way to just re-direct the events somehow to whatever handles the title bar dragging?

二,有没有一个事件处理程序适用于窗口中的所有元素的方法吗?如,使窗口拖动无论哪个元素用户点击拖动+。显然,无需手动添加处理程序,为每一个元素。只要做到这一点,一旦某个地方?

Second, is there a way to apply an event handler to all elements in the window? As in, make the window draggable no matter which element the user click+drags. Obviously without adding the handler manually, to every single element. Just do it once somewhere?

推荐答案

当然,应用以下的MouseDown 你的窗口

private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
    if (e.ChangedButton == MouseButton.Left)
        this.DragMove();
}

这将允许用户拖动窗口当他们点击/拖动任何控制,除了它吃MouseDown事件控件( e.Handled = TRUE

This will allow users to drag the Window when they click/drag on any control, EXCEPT for controls which eat the MouseDown event (e.Handled = true)

您可以使用 PreviewMouseDown 而不是的MouseDown ,但拖动事件吃点击事件,所以你的窗口停止响应鼠标左键单击事件。如果你真的想成为能够从任何控制单击并拖动表格,你很可能使用 PreviewMouseDown ,启动一个定时器,开始拖动操作,并取消如果操作点¯x毫秒内的MouseUp 事件触发。

You can use PreviewMouseDown instead of MouseDown, but the drag event eats the Click event, so your window stops responding to left-mouse click events. If you REALLY wanted to be able to click and drag the form from any control, you could probably use PreviewMouseDown, start a timer to begin the drag operation, and cancel the operation if the MouseUp event fires within X milliseconds.

这篇关于让WPF窗口拖动的,不管是什么点击元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆