这对我不起作用,专注于单选按钮仅在按下Tab键时才有效!有谁知道如何解决?
void SelectPaymentModeView_Loaded(object sender, RoutedEventArgs e)
{
this.radPaymentMode.Focus();
}
radiobutton的内容是文本......我也尝试使用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));
}
答案 0 :(得分:0)
键盘焦点管理器使键盘用于制表控件时可以看到点状焦点装饰器(例如,当使用鼠标时,WPF想隐藏焦点矩形,因此视觉混乱程度较低)。
要强制它,请使用这样的代码(假设btnRadio是您的按钮):
FocusManager.SetFocusedElement(FocusManager.GetFocusScope(btnRadio), btnRadio);