简体   繁体   中英

FocusVisualStyle In RadioButton not Work

This is not working for me, focus on radio button only works when pressed the Tab key! Does anyone know how to fix?

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

The contents of the radiobutton is text... I also try Keyboard.Focus(this.radPaymentMode);


See the complete code:

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

The keyboard focus manager makes the dotted focus adorner visible when the keyboard is used to tab to a control (WPF wants to hide the focus rect when the mouse is used for example so there's less visual clutter).

To force it, use code like this (assuming btnRadio is your button):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM