Exemplo n.º 1
0
        protected override void OnKeyDown(KeyEventArgs e)
        {
            try
            {
                MultiSelectTreeViewItem itemToSelect = null;

                if (e.Key == Key.Left)
                {
                    IsExpanded = false;
                    e.Handled  = true;
                }
                else if (e.Key == Key.Right)
                {
                    IsExpanded = true;
                    e.Handled  = true;
                }
                else if (e.Key == Key.Up)
                {
                    // In this case we need to select the last child of the last expandend node of
                    // - the previous at the same level (if this index node is NOT 0)
                    // - the parent node (if this index node is 0)

                    int currentNodeIndex = ParentItemsControl.ItemContainerGenerator.IndexFromContainer(this);

                    if (currentNodeIndex == 0)
                    {
                        itemToSelect = ParentMultiSelectTreeViewItem;
                    }
                    else
                    {
                        MultiSelectTreeViewItem tmp = null;
                        tmp          = GetPreviousNodeAtSameLevel(this);
                        itemToSelect = GetLastVisibleChildNodeOf(tmp);
                    }
                    e.Handled = true;
                }
                else if (e.Key == Key.Down)
                {
                    // In this case we need to select:
                    // - the first child node (if this node is expanded)
                    // - the next at the same level (if this not the last child)
                    // - the next at the same level of the parent node (if this is the last child)

                    if (IsExpanded && Items.Count > 0)
                    { // Select first Child
                        itemToSelect = ItemContainerGenerator.ContainerFromIndex(0) as MultiSelectTreeViewItem;
                    }
                    else
                    {
                        itemToSelect = GetNextNodeAtSameLevel(this);

                        if (itemToSelect == null) // current node has no subsequent node at the same level
                        {
                            MultiSelectTreeViewItem tmp = ParentMultiSelectTreeViewItem;

                            while (itemToSelect == null && tmp != null) // searhing for the first parent that has a subsequent node at the same level
                            {
                                itemToSelect = GetNextNodeAtSameLevel(tmp);
                                tmp          = tmp.ParentMultiSelectTreeViewItem;
                            }
                        }
                    }
                    e.Handled = true;
                }

                if (itemToSelect != null)
                {
                    itemToSelect.Focus();
                    itemToSelect.IsSelected = true;
                    ParentMultiSelectTreeView.OnItemClicked(itemToSelect);
                }
            }
            catch (Exception) { /* Silently ignore */ }
        }