This problem relates mainly to context menus, but in my specific case it's about a TreeView control.
The TreeView item contains a StackPanel, and on that StackPanel is a ContextMenu property, which I have assigned to a StaticResource (which is a ContextMenu of course). Said ContextMenu leads to an ICommand and, thus, does its thing.
At present (and this is the default behaviour I believe), right clicking on an item in the TreeView does not select that item. This is common in Windows, but doesn't happen here. I would like it to happen (but I don't know how).
A little follow up information: I do have a selected item in the TreeView and this changes with a mouse left click. It's not a left click event, though, rather the event is 'SelectedItemChanged'. This leads to a method whereby I set the 'SelectedItem' in my data context (view model) to the SelectedItem. It has to be done this way because a TreeView's selected item is 'read only'.
That code is here, although I'm not sure how useful it is to the issue at hand:
private void TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
if (this.ScenesTreeView01 == null)
return;
if (this.ScenesTreeView01.DataContext == null)
return;
var DataContext = this.ScenesTreeView01.DataContext as ScenesViewModel;
if (e.NewValue is SceneViewModel)
{
DataContext.SelectedScene = (SceneViewModel)e.NewValue;
}
if (e.NewValue is CharacterViewModel)
{
DataContext.SelectedCharacter = (CharacterViewModel)e.NewValue;
}
}
Since there doesn't seem to be a place where it says 'okay you left clicked and so here is the selected item', I don't know what to do to tell it to assign the selected item on a right click (as well as a left click).
How can I do this?
Edit: I am using MVVM so when we have a method like SelectedItemChanged with a parameter which is RoutedPropertyChangedEventArgs e, e.Source refers me back to my view model, not to a TreeViewItem.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…