2017-10-16 90 views
0

現在我正在使用MouseLeftButtonUp事件檢測網格中的點擊次數。沒有兩個回調的C#wpf網格點擊事件

但是,每當我打開一個OpenFileDialog並選擇一個文件。鼠標上移事件通過對話框並在我的網格上註冊一個鼠標上移事件。

如何在我的網格上檢測有效的鼠標點擊(鼠標向下並向上移動鼠標),而無需創建多個回調來跟蹤我的元素是否實際單擊並且不會在鼠標懸停時發生?

例子:

System.Windows.Forms.OpenFileDialog o; 


o = new System.Windows.Forms.OpenFileDialog(); 

if (o.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
{ 

} 

當我雙擊對話框中的文件和我的格子窗後面。鼠標向上事件被調用。

+0

你有代碼示例,所以這種行爲可以被複制? –

+0

@GingerNinja添加了一個片段 – John

+0

那麼你的XAML,網格等等。能夠看到問題是否可以被複制將是很好的。 –

回答

2

有沒有簡單的方法來處理Click事件沒有兩個回調在Grid。但是我們可以編寫一些幫助代碼來實現它。

這是你可以使用我的助手代碼:以上

<Grid Background="Transparent"> 
    <local:RoutedEventExtension.Event> 
     <local:ClickEvent Click="Grid_Click"></local:ClickEvent> 
    </local:RoutedEventExtension.Event> 
</Grid> 

XAML。

private void Grid_Click(object sender, EventArgs e) 
{ 
    // Write your event handler code here. 
} 

C#以上。

這是我的助手代碼:

public abstract class RoutedEventExtension 
{ 
    public static readonly DependencyProperty EventProperty = DependencyProperty.RegisterAttached(
     "Event", typeof(RoutedEventExtension), typeof(RoutedEventExtension), 
     new PropertyMetadata(null, OnEventChanged)); 

    public static void SetEvent(DependencyObject element, RoutedEventExtension value) 
    { 
     element.SetValue(EventProperty, value); 
    } 

    public static RoutedEventExtension GetEvent(DependencyObject element) 
    { 
     return (RoutedEventExtension) element.GetValue(EventProperty); 
    } 

    private static void OnEventChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     if (!(d is IInputElement element)) 
     { 
      throw new InvalidOperationException("RoutedEventExtension can only be attached on an IInputElement."); 
     } 

     var oldValue = (RoutedEventExtension) e.OldValue; 
     var newValue = (RoutedEventExtension) e.NewValue; 

     oldValue?.Detach(); 
     newValue.Attach(element); 
    } 

    protected IInputElement Target { get; private set; } 

    private void Attach(IInputElement target) 
    { 
     Target = target; 
     OnAttached(); 
    } 

    private void Detach() 
    { 
     try 
     { 
      OnDetaching(); 
     } 
     finally 
     { 
      Target = null; 
     } 
    } 

    protected abstract void OnAttached(); 

    protected abstract void OnDetaching(); 
} 

public sealed class ClickEvent : RoutedEventExtension 
{ 
    public event EventHandler Click; 

    protected override void OnAttached() 
    { 
     Target.MouseLeftButtonDown += OnMouseLeftButtonDown; 
     Target.MouseLeftButtonUp += OnMouseLeftButtonUp; 
     Target.LostMouseCapture += OnLostMouseCapture; 
    } 

    protected override void OnDetaching() 
    { 
     Target.MouseLeftButtonDown -= OnMouseLeftButtonDown; 
     Target.MouseLeftButtonUp -= OnMouseLeftButtonUp; 
     Target.LostMouseCapture -= OnLostMouseCapture; 
    } 

    private bool _isMouseDown; 

    private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
    { 
     _isMouseDown = true; 
     Mouse.Capture(Target); 
    } 

    private void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e) 
    { 
     if (!_isMouseDown) 
     { 
      return; 
     } 

     Mouse.Capture(null); 
     OnClick(); 
    } 

    private void OnLostMouseCapture(object sender, MouseEventArgs e) 
    { 
     _isMouseDown = false; 
    } 

    private void OnClick() 
    { 
     Click?.Invoke(this, EventArgs.Empty); 
    } 
} 

助手代碼包含兩個班。一種提供一種常見的方式來附加IInputElement上的任何事件,另一種提供基本的Click實現。

您可以通過繼承自RoutedEventExtension自己編寫自己的其他事件實現。