我需要能够在WPF StackPanel上处理双击和单击事件。但是没有StackPanel的DoubleClick事件。 我想在这两个EventHandler中做两个不同的操作。
知道怎么做吗?
谢谢
答案 0 :(得分:24)
<StackPanel MouseDown="StackPanel_MouseDown">
<!--stackpanel content-->
<TextBlock>Hello</TextBlock>
</StackPanel>
然后在事件处理程序中:
private void StackPanel_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount >= 2)
{
string hello; //only hit here on double click
}
}
应该有效。请注意,在StackPanel中单击会触发事件(但是if if检查失败)
答案 1 :(得分:6)
$sid = '239';
答案 2 :(得分:5)
最好的方法是使用超时来控制自己的鼠标按钮处理程序 - 如果事件在超时期限内再次触发,则触发双击消息,否则调用单击处理程序。以下是一些示例代码(编辑:最初找到here):
/// <summary>
/// For double clicks
/// </summary>
public class MouseClickManager {
private event MouseButtonEventHandler _click;
private event MouseButtonEventHandler _doubleClick;
public event MouseButtonEventHandler Click {
add { _click += value; }
remove { _click -= value; }
}
public event MouseButtonEventHandler DoubleClick {
add { _doubleClick += value; }
remove { _doubleClick -= value; }
}
/// <summary>
/// Gets or sets a value indicating whether this <see cref="MouseClickManager"/> is clicked.
/// </summary>
/// <value><c>true</c> if clicked; otherwise, <c>false</c>.</value>
private bool Clicked { get; set; }
/// <summary>
/// Gets or sets the timeout.
/// </summary>
/// <value>The timeout.</value>
public int DoubleClickTimeout { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="MouseClickManager"/> class.
/// </summary>
/// <param name="control">The control.</param>
public MouseClickManager(int doubleClickTimeout) {
this.Clicked = false;
this.DoubleClickTimeout = doubleClickTimeout;
}
/// <summary>
/// Handles the click.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
public void HandleClick(object sender, MouseButtonEventArgs e) {
lock (this) {
if (this.Clicked) {
this.Clicked = false;
OnDoubleClick(sender, e);
}
else {
this.Clicked = true;
ParameterizedThreadStart threadStart = new ParameterizedThreadStart(ResetThread);
Thread thread = new Thread(threadStart);
thread.Start(e);
}
}
}
/// <summary>
/// Resets the thread.
/// </summary>
/// <param name="state">The state.</param>
private void ResetThread(object state) {
Thread.Sleep(this.DoubleClickTimeout);
lock (this) {
if (this.Clicked) {
this.Clicked = false;
OnClick(this, (MouseButtonEventArgs)state);
}
}
}
/// <summary>
/// Called when [click].
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
private void OnClick(object sender, MouseButtonEventArgs e) {
if (_click != null) {
if (sender is Control) {
(sender as Control).Dispatcher.BeginInvoke(_click, sender, e);
}
}
}
/// <summary>
/// Called when [double click].
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
private void OnDoubleClick(object sender, MouseButtonEventArgs e) {
if (_doubleClick != null) {
_doubleClick(sender, e);
}
}
}
然后,在控件中,您希望接收事件:
MouseClickManager fMouseManager = new MouseClickManager(200);
fMouseManager.Click += new MouseButtonEventHandler(YourControl_Click);
fMouseManager.DoubleClick += new MouseButtonEventHandler(YourControl_DoubleClick);
答案 3 :(得分:0)
我遇到了类似的问题(响应单击事件并在双击事件的情况下再做一次工作)。我用这种方式解决了这个问题:
1)定义并声明一些整数来保存最终点击时间戳
int lastClickTimestamp;
2)在Window_Loaded方法中初始化先前声明的变量,其中某个数字大于200
lastClickTimestamp = 1000;
3)将鼠标按钮处理程序添加到堆栈面板
stackPanel.MouseLeftButtonUp += new MouseButtonEventHandler(stackPanel_MouseLeftButtonUp);
4)添加以下方法
void stackPanel_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (e.Timestamp - lastClickTimeStamp < 200)
{
//double click
}
lastClickTimeStamp = e.Timestamp;
//single click
}
如果您需要单独检测单击和双击事件,则此代码无效。这种情况引入了更多的复杂性,但绝对可以解决。
答案 4 :(得分:0)
使用WPF DispatcherTimer
完成答案。我们按需创建计时器并在完成后断开连接以防止资源堵塞。
C#:
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
public partial class MainWindow : Window
{
DispatcherTimer dt;
bool clear_timer()
{
if (dt == null)
return false;
dt.Tick -= _single_click;
dt = null;
return true;
}
private void _click(Object sender, MouseButtonEventArgs e)
{
if (clear_timer())
Debug.Print("double click");
else
dt = new DispatcherTimer(
TimeSpan.FromMilliseconds(GetDoubleClickTime()),
DispatcherPriority.Normal,
_single_click,
Dispatcher);
}
void _single_click(Object sender, EventArgs e)
{
clear_timer();
Debug.Print("single click");
}
public MainWindow() { InitializeComponent(); }
[DllImport("user32.dll")]
static extern uint GetDoubleClickTime();
};
XAML:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel Orientation="Horizontal"
Background="AliceBlue"
Width="100"
Height="100"
MouseLeftButtonDown="_click" />
</Window>
答案 5 :(得分:0)
有一个更简单的解决方案可以做到这一点。
在StackPanel的事件PreviewMouseLeftDown(例如)中,您可以检查MouseButtonEventArgs.ClickCount属性的值是否为2。 1 =单击 2 =双击