WPF StackPanel でダブルクリック イベントとシングル クリックイベントを処理できる必要があります。しかし、StackPanel の DoubleClick イベントのようなものはありません。これら 2 つの EventHandler で 2 つの異なる操作を実行したいと考えています。
それを行う方法はありますか?
ありがとうございました
WPF StackPanel でダブルクリック イベントとシングル クリックイベントを処理できる必要があります。しかし、StackPanel の DoubleClick イベントのようなものはありません。これら 2 つの EventHandler で 2 つの異なる操作を実行したいと考えています。
それを行う方法はありますか?
ありがとうございました
<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 を 1 回クリックするとイベントが発生することに注意してください (ただし、if チェックは失敗します)。
最善の方法は、タイムアウトを指定して独自のマウス ボタン ハンドラーを作成することです。タイムアウト期間内にイベントが再度発生した場合は、ダブルクリック メッセージを発生させます。それ以外の場合は、シングル クリック ハンドラーを呼び出します。ここにいくつかのサンプルコードがあります(編集:もともとここにありました):
/// <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);
同様の問題がありました(シングルクリックイベントに応答し、ダブルクリックイベントの場合は追加の作業を行います)。私はこの方法で問題を解決しました:
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
}
シングルクリックイベントとダブルクリックイベントを別々に検出する必要がある場合、このコードは役に立ちません。その状況はもう少し複雑になりますが、間違いなく解決できます。
これを行うためのより簡単な解決策があります。
StackPanel のイベント PreviewMouseLeftDown (たとえば) で、MouseButtonEventArgs.ClickCount プロパティの値が 2 かどうかを確認できます。1 = シングルクリック 2 = ダブルクリック
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>