我目前正在尝试将焦点设置在我的树视图的选定值上,然后显示后面代码中内置的上下文菜单。
上下文菜单的焦点和显示都可以单独使用,但是当我在同一个块中时,只有焦点触发并且上下文菜单没有显示?
我想找出为什么我的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 :(得分:0)
我发现为什么它没有处理这两个事件,行
e.Handled = true;
阻止了其余的代码被执行,因为MouseButtonEventArgs已经被handeled所以代码不会打扰执行其余代码。
删除上面的代码后,一切正常。