protected override DependencyObject GetContainerForItemOverride() { TreeListViewItem item = new TreeListViewItem(); item.GridViewColumns = GridViewColumns; return(item); }
private bool ExpandAndSelectItem(ItemsControl parentContainer, object itemToSelect) { foreach (Object item in parentContainer.Items) { count++; TreeListViewItem currentContainer = parentContainer.ItemContainerGenerator.ContainerFromItem(item) as TreeListViewItem; if (item == itemToSelect && currentContainer != null) { currentContainer.IsSelected = true; currentContainer.BringIntoView(); currentContainer.Focus(); ScrollViewer viewer = VisualUpwardSearch <ScrollViewer>(currentContainer as DependencyObject) as ScrollViewer; viewer.ScrollToVerticalOffset(count * 25); return(true); } if (currentContainer != null && currentContainer.Items.Count > 0) { bool wasExpanded = currentContainer.IsExpanded; currentContainer.IsExpanded = true; currentContainer.UpdateLayout(); if (ExpandAndSelectItem(currentContainer, itemToSelect)) { return(true); } else { currentContainer.IsExpanded = wasExpanded; currentContainer.UpdateLayout(); } } } return(false); }
protected override void OnPreviewMouseRightButtonDown(MouseButtonEventArgs e) { TreeListViewItem tvitem = VisualUpwardSearch <TreeListViewItem>(e.OriginalSource as DependencyObject) as TreeListViewItem; if (tvitem != null) { tvitem.Focus(); } base.OnPreviewMouseRightButtonDown(e); }
static TreeListView GetTree(TreeListViewItem item) { Func <DependencyObject, DependencyObject> getParent = (o) => VisualTreeHelper.GetParent(o); FrameworkElement currentItem = item; while (!(getParent(currentItem) is TreeListView)) { currentItem = (FrameworkElement)getParent(currentItem); } return((TreeListView)getParent(currentItem)); }
private TreeListViewItem FindTreeViewItem(ItemsControl item, object data) { TreeListViewItem findItem = null; bool itemIsExpand = false; if (item is TreeListViewItem) { TreeListViewItem tviCurrent = item as TreeListViewItem; object itemData = tviCurrent.DataContext; if (itemData == data) { findItem = tviCurrent; } else { itemIsExpand = tviCurrent.IsExpanded; if (!tviCurrent.IsExpanded) { tviCurrent.SetValue(TreeViewItem.IsExpandedProperty, true); tviCurrent.UpdateLayout(); } } } if (findItem == null) { for (int i = 0; i < item.Items.Count; i++) { TreeListViewItem tvItem = (TreeListViewItem)item.ItemContainerGenerator.ContainerFromIndex(i); if (tvItem == null) { continue; } object itemData = item.Items[i]; if (itemData == data) { findItem = tvItem; break; } else if (tvItem.Items.Count > 0) { findItem = FindTreeViewItem(tvItem, data); if (findItem != null) { break; } } } TreeListViewItem tviCurrent = item as TreeListViewItem; tviCurrent.SetValue(TreeListViewItem.IsExpandedProperty, itemIsExpand); tviCurrent.UpdateLayout(); } return(findItem); }
public void SetScorllViewPosition(object item, object oneLevelTree = null) { if (item == null) { return; } TreeListViewItem container = null; if (oneLevelTree == null) { foreach (var tree in this.Items) { TreeListViewItem tlvi = this.ItemContainerGenerator.ContainerFromItem(tree) as TreeListViewItem; container = FindTreeViewItem(tlvi, item) as TreeListViewItem; if (container != null) { break; } } if (container == null) { return; } container.BringIntoView(); container.IsSelected = true; } else { TreeListViewItem tlvi = this.ItemContainerGenerator.ContainerFromItem(oneLevelTree) as TreeListViewItem; if (item == oneLevelTree) { tlvi.IsSelected = true; tlvi.BringIntoView(); tlvi.Focus(); } else { tlvi.IsExpanded = true; tlvi.UpdateLayout(); count = 0; foreach (Object itemTree in this.Items) { count++; if (itemTree == oneLevelTree) { break; } } ExpandAndSelectItem(tlvi, item); } } }
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; } }
private static void MakeAnchorSelection(TreeListView tree, TreeListViewItem actionItem, bool clearCurrent) { if (GetAnchorItem(tree) == null) { var selectedItems = GetSelectedTreeViewItems(tree); if (selectedItems.Count > 0) { SetAnchorItem(tree, selectedItems[selectedItems.Count - 1]); } else { SetAnchorItem(tree, GetExpandedTreeViewItems(tree).Skip(3).FirstOrDefault()); } if (GetAnchorItem(tree) == null) { return; } } var anchor = GetAnchorItem(tree); var items = GetExpandedTreeViewItems(tree); bool betweenBoundary = false; foreach (var item in items) { bool isBoundary = item == anchor || item == actionItem; if (isBoundary) { betweenBoundary = !betweenBoundary; } if (betweenBoundary || isBoundary) { SetIsSelected(item, true); } else { if (clearCurrent) { SetIsSelected(item, false); } else { break; } } } }
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); }
private static void MakeSingleSelection(TreeListView tree, TreeListViewItem item) { foreach (TreeListViewItem selectedItem in GetExpandedTreeViewItems(tree)) { if (selectedItem == null) { continue; } if (selectedItem != item) { SetIsSelected(selectedItem, false); } else { SetIsSelected(selectedItem, true); } } UpdateAnchorAndActionItem(tree, item); }
protected override void OnExpanded(RoutedEventArgs e) { base.OnExpanded(e); try { TreeListViewItem t = e.Source as TreeListViewItem; if (t == null) { return; } ControlTemplate template = t.Template; Storyboard show = template.Resources["STShow"] as Storyboard; ItemsPresenter item = template.FindName("ItemsHost", t) as ItemsPresenter; show.Begin(item); } catch (Exception ex) { //System.Windows.MessageBox.Show(ex.Message); } }
static void RealSelectedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args) { TreeListViewItem item = (TreeListViewItem)sender; var selectedItems = GetSelectedItems(GetTree(item)); if (selectedItems != null) { var isSelected = GetIsSelected(item); if (isSelected) { try { selectedItems.Add(item.Header); } catch (ArgumentException) { } } else { selectedItems.Remove(item.Header); } } }
private static void UpdateAnchorAndActionItem(TreeListView tree, TreeListViewItem item) { SetAnchorItem(tree, item); }
private static void MakeToggleSelection(TreeListView tree, TreeListViewItem item) { SetIsSelected(item, !GetIsSelected(item)); UpdateAnchorAndActionItem(tree, item); }