Пример #1
0
        // When focus comes from outside the RadioButtons control we will put focus on the selected radio button.
        private void OnGettingFocus(object sender, GettingFocusEventArgs args)
        {
            var repeater = m_repeater;

            if (repeater != null)
            {
                var inputDevice = args.InputDevice;
                if (inputDevice == FocusInputDeviceKind.Keyboard)
                {
                    // If focus is coming from outside the repeater, put focus on the selected item.
                    var oldFocusedElement = args.OldFocusedElement;
                    if (oldFocusedElement == null || repeater != VisualTreeHelper.GetParent(oldFocusedElement))
                    {
                        var selectedItem = repeater.TryGetElement(m_selectedIndex);
                        if (selectedItem != null)
                        {
                            var argsAsIGettingFocusEventArgs2 = args as GettingFocusEventArgs;
                            if (argsAsIGettingFocusEventArgs2 != null)
                            {
                                if (args.TrySetNewFocusedElement(selectedItem))
                                {
                                    args.Handled = true;
                                }
                            }
                        }
                    }

                    // Focus was already in the repeater: in On RS3+ Selection follows focus unless control is held down.
                    else if (SharedHelpers.IsRS3OrHigher() &&
                             (Windows.UI.Xaml.Window.Current.CoreWindow.GetKeyState(VirtualKey.Control) &
                              CoreVirtualKeyStates.Down) != CoreVirtualKeyStates.Down)
                    {
                        var newFocusedElementAsUIE = args.NewFocusedElement as UIElement;
                        if (newFocusedElementAsUIE != null)
                        {
                            Select(repeater.GetElementIndex(newFocusedElementAsUIE));
                            args.Handled = true;
                        }
                    }
                }
            }
        }
 bool IGettingFocusEventArgsResolver.TrySetNewFocusedElement(GettingFocusEventArgs e, DependencyObject element) => e.TrySetNewFocusedElement(element);