0

这对我不起作用,仅在按下 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));
    }
4

1 回答 1

0

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

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

    FocusManager.SetFocusedElement(FocusManager.GetFocusScope(btnRadio), btnRadio);
于 2012-05-08T23:40:32.187 回答