2010-12-20 61 views
4

我需要做一些像谷歌地圖做(但WPF):調用一個文本菜單打開

  • RightClick地圖我有一個文本菜單上。
  • RightDoubleClicking我有UnZoom動作。

所以,很顯然,這是在WPF ... 有點困難掙扎,尋找了很多後,讀人們孔潘說:「我們無法預測未來」(我問自己如何預測谷歌它),我決定「等待」SystemInformation.DoubleClickTime然後才顯示一個contextMenu。

當然,這是不理想的,甚至人類可觀察,但我不知道其他方法。

所以,我有下面的代碼的問題是(我有一個自定義畫布):

ContextMenuEventArgs lastContextMenuEventArgs = null; 
bool? lastContextMenuEventArgsHandled = null; 

protected override void OnContextMenuOpening(ContextMenuEventArgs e) 
{ 
    lastContextMenuEventArgs = e; 
    lastContextMenuEventArgsHandled = e.Handled; 

    e.Handled = true; 
    //base.OnContextMenuOpening(e); 
} 

bool rightMouseClickedOnce = false; 
protected override void OnPreviewMouseRightButtonUp(MouseButtonEventArgs e) 
{ 
    //base.OnPreviewMouseRightButtonUp(e); 

    Console.WriteLine(">>>>>>>>>>> OnPreviewMouseRightButtonUp"); 
    if (!rightMouseClickedOnce) 
    { 
     rightMouseClickedOnce = true; 
     Thread thread = new Thread(
      new System.Threading.ThreadStart(
       delegate() 
       { 
        Thread.Sleep(System.Windows.Forms.SystemInformation.DoubleClickTime); 
        this.Dispatcher.Invoke(
         System.Windows.Threading.DispatcherPriority.Background, 
         new Action(
          delegate() 
          { 
           if (rightMouseClickedOnce) 
           { 
            Console.WriteLine(">>>>>>>>>>> Right Click"); 
            rightMouseClickedOnce = false; 
            base.OnPreviewMouseRightButtonUp(e); 

            if (lastContextMenuEventArgsHandled.HasValue) 
            { 
             Console.WriteLine(">>>>>>>>>>> lastContextMenuEventArgsHandled"); 
             lastContextMenuEventArgs.Handled = lastContextMenuEventArgsHandled.Value; 
             base.OnContextMenuOpening(lastContextMenuEventArgs); 
             lastContextMenuEventArgsHandled = null; 
            } 
            //if (this.ContextMenu != null) 
            //{ 
            // this.ContextMenu.PlacementTarget = this; 
            // this.ContextMenu.IsOpen = true; 
            //} 
           } 
          } 
        )); 
       } 
     )); 
     thread.Start(); 
    } 
    else if (rightMouseClickedOnce) 
    { 
     Console.WriteLine(">>>>>>>>>>> Right Double Click"); 
     rightMouseClickedOnce = false; 
     base.OnPreviewMouseRightButtonUp(e); 
     this.OnMouseRightDoubleClick(e); 
    } 

} 

一切都很正常,但是有一點問題:base.OnContextMenuOpening(lastContextMenuEventArgs);似乎不工作...

if (this.ContextMenu != null) 
{ 
    this.ContextMenu.PlacementTarget = this; 
    this.ContextMenu.IsOpen = true; 
} 

前設置和工作,但最後這個塊子從開放,開放總是父(畫布)的ContextMenu的ContextMenu元素。

我可以直接調用contextMenu事件嗎?

回答

0

我對WPF瞭解不多,但是可以顯示菜單,比如遠離光標的一個像素,看看用戶是否再次在相同位置和系統雙擊時間內右鍵單擊?我曾經在遊戲中使用過類似的技術,並且表現得足夠好。
一個菜單「淡入」(可選地,也是)動畫可以使它更好。

+0

問題是:a)相關菜單在鼠標位置自動打開(這是因爲我阻止了上下文菜單打開); b)我不應該阻止子元素上下文菜單(比如說,一個City contextMenu或RoadContemtMenu)。兒童也通過相同的事件conteextMenuOpening ... – serhio 2010-12-20 19:35:53

+0

然後隱藏它時,第二次右鍵單擊完成。編輯:嗯... – Vercas 2010-12-20 19:38:41

相關問題