2012-05-08 49 views
0

這不適用於我,專注於單選按鈕只有在按下Tab鍵時才起作用!有誰知道如何解決?FocusVisualStyle在RadioButton中不工作

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)); 
    } 

回答

0

鍵盤焦點管理器使點對焦裝飾器時可見鍵盤用於標籤來控制(WPF想要隱藏焦點矩形當鼠標例如用於減少視覺混亂)。

要強制它,使用這樣的代碼(假設btnRadio是你的按鈕):

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

我的單選按鈕具有焦點!但他沒有申請我的FocusVisualStyle,只適用於當我按Tab時,它不起作用。 –

+0

這是故意的 - 只有從鍵盤輸入到控件的用戶才能看到焦點矩形。如果你想自己強迫你,你需要像我展示的東西。 – jschroedl

+0

但您顯示的代碼對我無效! –

相關問題