【问题标题】:FocusVisualStyle In RadioButton not Work单选按钮中的 FocusVisualStyle 不起作用
【发布时间】:2012-05-17 10:17:33
【问题描述】:

这对我不起作用,仅在按下 Tab 键时专注于单选按钮!有人知道怎么解决吗?

 void SelectPaymentModeView_Loaded(object sender, RoutedEventArgs e)
    {
        this.radPaymentMode.Focus(); 
    }

单选按钮的内容是文本...我也试试 Keyboard.Focus(this.radPaymentMode);


查看完整代码:

PaymentMode[] modes = data[1] as PaymentMode[];
if (modes.Length > 0)
{
    for (int i = 0; i < modes.Length; i++)
    {
        RadioButton rad = new RadioButton();

        rad.Name = "radPayment" + i;
        rad.GroupName = "PaymentModes";
        rad.Content = modes[i].Name;
        rad.DataContext = modes[i];
        rad.Margin = new Thickness(110, 0, 0, 5);
        rad.VerticalAlignment = System.Windows.VerticalAlignment.Center;
        rad.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
        Grid.SetRow(rad, 3 + i);
        Grid.SetColumn(rad, 1);
        gridPaymentModes.RowDefinitions.Insert(3, new RowDefinition());
        gridPaymentModes.Children.Add(rad);
        radPaymentModes.Add(rad);

        if (!string.IsNullOrEmpty((this.DataContext as Order).Payment))
        {
            String paymentOrder = rad.Content as String;
            if (paymentOrder.Equals((this.DataContext as Order).Payment))
            {
                rad.IsChecked = true;
            }
        }

        rad.Checked += new RoutedEventHandler(rad_Checked);
    }
    radPaymentModes[0].Loaded += SelectPaymentModeView_Loaded;
}

 void SelectPaymentModeView_Loaded(object sender, RoutedEventArgs e)
    {
        FocusManager.SetFocusedElement(FocusManager.GetFocusScope((sender as RadioButton)), (sender as RadioButton));
    }

【问题讨论】:

    标签: wpf tabs focus styles radio-button


    【解决方案1】:

    键盘焦点管理器使虚线焦点装饰器在使用键盘选项卡到控件时可见(例如,WPF 希望在使用鼠标时隐藏焦点矩形,以减少视觉混乱)。

    要强制它,请使用这样的代码(假设 btnRadio 是您的按钮):

        FocusManager.SetFocusedElement(FocusManager.GetFocusScope(btnRadio), btnRadio);
    

    【讨论】:

    • 我的单选按钮有焦点!但是他没有应用我的 FocusVisualStyle,只有当我按 Tab 时才应用,它不起作用。
    • 这是故意的 - 只有从用户实际键盘输入到控件的焦点才会显示焦点矩形。如果你想自己强迫它,你需要像我展示的那样。
    猜你喜欢
    • 1970-01-01
    • 2017-07-13
    • 2018-01-17
    • 2013-06-22
    • 2017-02-02
    • 2015-11-15
    • 2016-01-06
    • 2014-05-06
    • 1970-01-01
    相关资源
    最近更新 更多