コード例 #1
0
ファイル: CategoryList.cs プロジェクト: dox0/DotNet471RS3
        private bool SelectDefaultPropertyHelper(string defaultPropertyName, bool firstTime)
        {
            if (string.IsNullOrEmpty(defaultPropertyName))
            {
                return(false);
            }

            ModelCategoryEntry defaultPropertyCategory;
            PropertyEntry      defaultProperty       = FindPropertyEntry(defaultPropertyName, out defaultPropertyCategory);
            CategoryEditor     defaultCategoryEditor = FindCategoryEditor(defaultProperty, defaultPropertyCategory);
            UIElement          element    = null;
            bool elementPendingGeneration = false;

            // Try to look up the correct UIElement to select
            //
            if (defaultCategoryEditor != null)
            {
                element = FindCategoryEditorVisual(defaultCategoryEditor, defaultPropertyCategory, out elementPendingGeneration);
            }
            else if (defaultProperty != null)
            {
                element = FindPropertyEntryVisual(defaultProperty, defaultPropertyCategory, out elementPendingGeneration);
            }
            else if (this.Count > 0)
            {
                // Nothing found, so select the first selectable thing in the list
                element = PropertySelection.FindNeighborSelectionStop <UIElement>(this, SearchDirection.Next);
                elementPendingGeneration = false;
            }

            // If the UIElement was found, select it.  Otherwise, if it should exist but it wasn't generated yet,
            // wait until it is and try again.
            //
            if (element != null)
            {
                SelectAndFocus(element, StealFocusMode.OnlyIfCategoryListHasFocusWithin);

                // Ensure that we are in Default SelectionMode because calling SelectAndFocus automatically switches us
                // to Sticky mode
                //
                ResetSelectionMode();
            }
            else if (elementPendingGeneration && firstTime)
            {
                // Set the firstTime flag to false, to prevent any infinite loops should things go wrong
                this.Dispatcher.BeginInvoke(
                    DispatcherPriority.Loaded,
                    new SelectDefaultPropertyHelperDelegate(SelectDefaultPropertyHelper),
                    defaultPropertyName,
                    false);
            }
            else if (elementPendingGeneration && !firstTime)
            {
                elementPendingGeneration = false;
            }

            return(element != null || elementPendingGeneration);
        }
コード例 #2
0
ファイル: CategoryList.cs プロジェクト: dox0/DotNet471RS3
        // Keyboard Navigation

        protected override void OnKeyDown(KeyEventArgs e)
        {
            // Intercept Up, Down, Left, Right key strokes and use them to navigate around the
            // the control
            //
            if (e.Key == Key.Left || e.Key == Key.Right || e.Key == Key.Up || e.Key == Key.Down)
            {
                if (_selection != null && !e.Handled)
                {
                    if (object.Equals(
                            Keyboard.FocusedElement,
                            VisualTreeUtils.FindFocusableElement <FrameworkElement>(_selection)))
                    {
                        ISelectionStop selectionStop = PropertySelection.GetSelectionStop(_selection);
                        if (selectionStop != null)
                        {
                            if (selectionStop.IsExpandable)
                            {
                                if ((e.Key == Key.Right && !selectionStop.IsExpanded) ||
                                    (e.Key == Key.Left && selectionStop.IsExpanded))
                                {
                                    selectionStop.IsExpanded = !selectionStop.IsExpanded;
                                    e.Handled = true;
                                }
                            }
                        }

                        if (!e.Handled)
                        {
                            SearchDirection  direction = e.Key == Key.Up || e.Key == Key.Left ? SearchDirection.Previous : SearchDirection.Next;
                            FrameworkElement nextStop  = PropertySelection.FindNeighborSelectionStop <FrameworkElement>(_selection, direction);

                            // If need to select something, select it
                            if (nextStop != null)
                            {
                                SelectAndFocus(nextStop, StealFocusMode.Always);
                            }

                            e.Handled = true;
                        }
                    }
                }
            }

            base.OnKeyDown(e);
        }