Right Mouse Click Selection

Feb 5, 2014 at 6:31 AM
Edited Feb 5, 2014 at 7:04 AM
Hi, when using the Tree's Context Menu I want to first select the particular node without manually left click it before. First I tried to implement that with interaction triggers:
<i:EventTrigger EventName="PreviewMouseRightButtonDown">
            <ei:ChangePropertyAction PropertyName="IsSelected" Value="true" TargetObject="{Binding}"/>
          </i:EventTrigger>
with the result that the right clicked node is additionally selected not single selected.
I only achieved it with modifying the TreeViewEx's OnMouseDown method
if (e.ChangedButton != MouseButton.Left && e.ChangedButton != MouseButton.Right) return;
But of course, if possible I want to avoid that

Any ideas on that? Thanks!
Coordinator
Feb 5, 2014 at 2:28 PM
Hi,

you might want to try sth like that:
<Style TargetType="Controls:TreeViewExItem">
    <EventSetter Event="MouseUp" Handler="MouseUp"/>
</Style>
private void MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    TreeViewExItem item = sender as TreeViewExItem;
     if (item != null 
          && e.ChangedButton == MouseButton.Right
          && (Keyboard.Modifiers & ModifierKeys.Control) != ModifierKeys.Control)
     {
         tree.SelectedItems.Clear();
         tree.SelectedItems.Add(item.DataContext);
         item.Focus();
         e.Handled = true;
      }
}
Regards!
Feb 6, 2014 at 6:46 AM
Thanks slompf for the quick response!
I added the overriden MouseUp function and altered it according to the MouseDown function in the SelectionMultiple.cs
internal override void OnMouseUp(MouseButtonEventArgs e)
        {
            TreeViewExItem item = GetTreeViewItemUnderMouse(e.GetPosition(treeViewEx));
            if (item != null && !item.IsEditing && e.ChangedButton == MouseButton.Right)
            {
                SelectSingleItem(item);

                FocusHelper.Focus(item);
            }

            base.OnMouseUp(e);
        }
I accept Keyboard Modifiers for multi selection.
Hopefully the next realease will implement that behavior, so that I don't have to update it in the future ;-)

Thanks a lot! Matthias
Feb 10, 2014 at 7:24 AM
Hi again,

I modified your SelectionMultiple.OnMouseDown handler to support context menu on selected items
internal override void OnMouseDown(MouseButtonEventArgs e)
        {
            TreeViewExItem item = GetTreeViewItemUnderMouse(e.GetPosition(treeViewEx));
            bool leftClick = e.ChangedButton == MouseButton.Left;
            bool rightClick = e.ChangedButton == MouseButton.Right;

            if (item != null && !item.IsEditing && (leftClick || rightClick))
            {
                if (leftClick)
                {
                    SelectSingleItem(item);
                    FocusHelper.Focus(item);
                    e.Handled = true;
                }
                else if (rightClick)
                {
                    if (!item.IsSelected)
                    {
                        SelectSingleItem(item);
                        FocusHelper.Focus(item);
                    }
                    e.Handled = true;
                }
            }

            base.OnMouseDown(e);
        }
If the item wasn't selected before, it's first beeing selected