コード例 #1
0
        /// <summary>
        /// Selects the specified item.
        /// </summary>
        /// <param name="treeViewItem">The item.</param>
        /// <param name="unselectOthers">if set to <c>true</c> [unselect other items].</param>
        public void Select(VirtualTreeViewItem treeViewItem, bool unselectOthers)
        {
            if (SelectionMode == VirtualTreeViewSelectionMode.None)
            {
                return;
            }

            var item = ItemContainerGenerator.ItemFromContainer(treeViewItem);

            // remove other items
            if (unselectOthers || SelectionMode == VirtualTreeViewSelectionMode.Single)
            {
                var otherSelection = SelectedItems.Where(s => !ReferenceEquals(s, item)).ToArray();
                foreach (var o in otherSelection)
                {
                    SelectedItems.Remove(o);
                }
            }

            if (IsKeyboardFocusWithin && !treeViewItem.IsKeyboardFocusWithin)
            {
                treeViewItem.Focus();
            }

            if (!SelectedItems.Contains(item))
            {
                SelectedItems.Add(item);
            }
            treeViewItem.IsSelected = true;
        }
コード例 #2
0
        /// <summary>
        /// Gets the depth of the given item.
        /// This is used by binding generated items
        /// </summary>
        /// <param name="treeViewItem">The tree view item.</param>
        /// <returns></returns>
        private int GetDepth(VirtualTreeViewItem treeViewItem)
        {
            int depth = -1; // starting from -1 here, cause the dataContext below will be non-null at least once

            for (var dataContext = treeViewItem.DataContext; dataContext != null; dataContext = FlatItemsSource.GetParent(dataContext))
            {
                depth++;
            }
            return(depth);
        }
コード例 #3
0
        /// <summary>
        /// Deselects the specified item.
        /// </summary>
        /// <param name="treeViewItem">The item.</param>
        public void Deselect(VirtualTreeViewItem treeViewItem)
        {
            if (!treeViewItem.IsSelected)
            {
                return;
            }
            var item = ItemContainerGenerator.ItemFromContainer(treeViewItem);

            SelectedItems.Remove(item);
            treeViewItem.IsSelected = false;
        }