static void ItemClicked(object sender, MouseButtonEventArgs e) { TreeListViewItem item = FindTreeViewItem(e.OriginalSource); if (item == null) { return; } TreeListView tree = (TreeListView)sender; var mouseButton = e.ChangedButton; if (mouseButton != MouseButton.Left) { if ((mouseButton == MouseButton.Right) && ((Keyboard.Modifiers & (ModifierKeys.Shift | ModifierKeys.Control)) == ModifierKeys.None)) { if (GetIsSelected(item)) { UpdateAnchorAndActionItem(tree, item); return; } MakeSingleSelection(tree, item); } return; } if ((Keyboard.Modifiers & (ModifierKeys.Shift | ModifierKeys.Control)) != (ModifierKeys.Shift | ModifierKeys.Control)) { if (GetIsSelected(item) && Keyboard.Modifiers != ModifierKeys.Shift && Keyboard.Modifiers != ModifierKeys.Control) { return; } else if (Keyboard.Modifiers != ModifierKeys.Shift && Keyboard.Modifiers != ModifierKeys.Control) { SelectedTreeItems.Clear(); } if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control) { MakeToggleSelection(tree, item); return; } if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift) { MakeAnchorSelection(tree, item, true); return; } MakeSingleSelection(tree, item); return; } }
public static void SetIsSelected(DependencyObject obj, bool value) { TreeListViewItem tlvi = obj as TreeListViewItem; //if (IsKeyCtrl && tlvi.IsSelected) //{ // return; //} if (value) { GradientStopCollection gradientStopCollection = new GradientStopCollection(); gradientStopCollection.Add(new GradientStop() { Color = (Color)ColorConverter.ConvertFromString("#FFC7DFFC"), Offset = 1 }); gradientStopCollection.Add(new GradientStop() { Color = (Color)ColorConverter.ConvertFromString("#FF3832B8"), Offset = 1 }); LinearGradientBrush brush = new LinearGradientBrush(gradientStopCollection, new Point(0.5, 0), new Point(0.5, 1)); tlvi.Background = brush; if (SelectedTreeItems == null) { SelectedTreeItems = new List <object>(); } if (!SelectedTreeItems.Contains(tlvi.DataContext)) { SelectedTreeItems.Add(tlvi.DataContext); } } else { tlvi.Background = Brushes.Transparent; if (SelectedTreeItems != null) { if (SelectedTreeItems.Contains(tlvi.DataContext)) { SelectedTreeItems.Remove(tlvi.DataContext); } } } obj.SetValue(IsSelectedProperty, value); }