WPF中RadioButton按钮扩展键盘操作

扩展类

 public class RadioButtonBehavior : Behavior<RadioButton>
    {
        public bool IsFocused
        //キャンセルボタンからのキャンセルかどうか
        {
            get { return (bool)GetValue(IsFocusedProperty); }
            set { SetValue(IsFocusedProperty, value); }
        }
       
        public static readonly DependencyProperty IsFocusedProperty = DependencyProperty.Register(
          "IsFocused", typeof(bool), typeof(RadioButtonBehavior),
          new PropertyMetadata(IsFocusedPropertyChanged));

        private static void IsFocusedPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            var p = dependencyObject as RadioButton;
            if (p == null) return;
            if ((e.NewValue is bool ? (bool)e.NewValue : false))
            {
                p.Focus();
            }
        }

        private static bool isArrowKeyPressed = false;

        protected override void OnAttached()
        {
            AppLog.Instance.LoggingInfo("[IN]");
            base.OnAttached();
            AssociatedObject.GotFocus += OnGotFocus;
            AssociatedObject.PreviewKeyDown += OnKeyDown;
            AssociatedObject.KeyUp += OnKeyUp;
            AppLog.Instance.LoggingInfo("[OUT]");
        }

        protected override void OnDetaching()
        {
            AppLog.Instance.LoggingInfo("[IN]");
            base.OnDetaching();
            AssociatedObject.GotFocus -= OnGotFocus;
            AssociatedObject.PreviewKeyDown -= OnKeyDown;
            AssociatedObject.KeyUp -= OnKeyUp;
            AppLog.Instance.LoggingInfo("[OUT]");
        }

        private void OnKeyDown(object sender, KeyEventArgs e)
        {
            AppLog.Instance.LoggingInfo("[IN]");
            isArrowKeyPressed = e.Key == Key.Up || e.Key == Key.Down;
            AppLog.Instance.LoggingInfo("[OUT]");
        }

        private void OnKeyUp(object sender, KeyEventArgs e)
        {
            AppLog.Instance.LoggingInfo("[IN]");
            isArrowKeyPressed = false;
            AppLog.Instance.LoggingInfo("[OUT]");
        }

        private void OnGotFocus(object sender, RoutedEventArgs e)
        {
            AppLog.Instance.LoggingInfo("[IN]");
            if (!(sender is RadioButton radioButton)) {
                AppLog.Instance.LoggingInfo("[OUT]");
                return;
            }
            //if (isArrowKeyPressed)
            //{
            //    radioButton.IsChecked = true;
            //}
            radioButton.IsChecked = true;
            AppLog.Instance.LoggingInfo("[OUT]");
        }
    }
View Code
 <RadioButton x:Name="uiuserdataSelRbtnFromFile"
                                         GroupName="uiuserdataImpexpSelection"
                                         Content="{x:Static const:Resources.SelectImportExportViewCheckBoxImport}"
                                         Style="{DynamicResource uiuserdataMiddleRadioButtonUI}"
                                         IsChecked="{Binding IsCheckedImportFromFile}"
                                         VerticalAlignment="Center"
                                         VerticalContentAlignment="Center">
                                <i:Interaction.Behaviors>
                                    <util:RadioButtonBehavior/>
                                </i:Interaction.Behaviors>
                            </RadioButton>
View Code

默认选中

 this.Loaded += (s, e) =>
            {
                if (uiuserdataSelRbtnFromFile.IsChecked == true)
                {
                    uiuserdataSelRbtnFromFile.Focus();
                }
                else
                {
                    uiuserdataSelRbtnToFile.Focus();
                }
            };
View Code

猜你喜欢

转载自www.cnblogs.com/ilison/p/10893637.html