2015-06-18 35 views
0

我目前正試圖將焦點設置在我的樹視圖的選定值上,然後在後面的代碼中顯示內置的菜單項。Treeviewitem焦點可以防止顯示上下文菜單

上下文菜單的焦點和顯示都單獨工作,但是當我同時在同一個塊中時,只有焦點會觸發並且上下文菜單不顯示?

我想知道爲什麼我的treeviewitem焦點阻止顯示上下文菜單。

這裏是我的onclick事件

private void TextBlock_MouseRightButtonUp(object sender, MouseButtonEventArgs e) 
    { 
     //searches up the tree until it finds the first treeviewitem and sets the focus to it. 
     TreeViewItem treeViewItem = VisualUpwardSearch(e.OriginalSource as DependencyObject); 

     if (treeViewItem != null) 
     { 
      treeViewItem.Focus(); 
      e.Handled = true; 
     } 

     tvProjects.Tag = treeViewItem.Header; 




     var fm = new FileMenu(); 
     var m = new MainViewModel(); 

     //Create new contextmenu and set datacontext and itemsource 
     ContextMenu cm = new ContextMenu(); 
     cm.DataContext = m; 
     cm.ItemsSource = m.MenuItems; 

     //set bindings through style 
     Style style = new Style(); 
     style.TargetType = typeof(MenuItem); 
     style.Setters.Add(new Setter(MenuItem.HeaderProperty, new Binding("Header"))); 
     style.Setters.Add(new Setter(MenuItem.ItemsSourceProperty, new Binding("Items"))); 
     style.Setters.Add(new Setter(MenuItem.CommandProperty, new Binding("Command"))); 
     style.Setters.Add(new Setter(MenuItem.CommandParameterProperty, new Binding("CommandParameter"))); 
     cm.ItemContainerStyle = style; 

     TextBlock tb = (TextBlock)sender; 

     //Show the menu 
     tb.ContextMenu = cm; 
     tb.ContextMenu.Visibility = System.Windows.Visibility.Visible; 
    } 

回答

0

我發現爲什麼它不處理這兩個事件,行

e.Handled = true; 

防止從beeing執行的代碼的其餘部分,因爲MouseButtonEventArgs有已經被處理了,所以代碼不會執行其餘的操作。

刪除上面的代碼行後,一切正常。