Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

RadioButton Class

Updated: April 2009

Represents a button that can be selected, but not cleared, by a user. The IsChecked property of a RadioButton can be set by clicking it, but it can only be cleared programmatically.

Namespace:  System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
[LocalizabilityAttribute(LocalizationCategory.RadioButton)]
public class RadioButton : ToggleButton
<RadioButton>
  Content
</RadioButton>

A RadioButton has two states: true or false.

Content Model: RadioButton is a ContentControl. Its content property is Content. For more information on the content model for RadioButton, see Controls Content Model Overview.

The RadioButton is a control that is usually used as an item in a group of RadioButton controls. However, it is possible to create a single RadioButton. Whether a RadioButton is selected is determined by the state of its IsChecked property.

When a RadioButton is selected, it cannot be cleared by clicking it. When RadioButton elements are grouped, the buttons are mutually exclusive. A user can select only one item at a time within a RadioButton group. You can group RadioButton controls by placing them inside a parent or by setting the GroupName property on each RadioButton.

Dependency properties for this control might be set by the control’s default style. If a property is set by a default style, the property might change from its default value when the control appears in the application. The default style is determined by which desktop theme is used when the application is running. For more information, see Themes.

The following example shows how to create RadioButton controls, group them inside a container, and handle the Checked event.

<StackPanel>
  <RadioButton Name="rb1" Checked="WriteText2">Yes</RadioButton>
  <RadioButton Name="rb2" Checked="WriteText2">No</RadioButton>
  <RadioButton Name="rb3" Checked="WriteText2">No opinion</RadioButton>
</StackPanel>
void WriteText2(object sender, RoutedEventArgs e)
{
    RadioButton li = (sender as RadioButton);
    txtb.Text = "You clicked " + li.Content.ToString() + ".";
}

The following code sample creates two separate RadioButton groups: colorgrp and numgrp. The user can choose one RadioButton in each group.

<StackPanel>
  <RadioButton GroupName="colorgrp">Red</RadioButton>
  <RadioButton GroupName="colorgrp">Blue</RadioButton>
  <RadioButton GroupName="numgrp">1</RadioButton>
  <RadioButton GroupName="numgrp">2</RadioButton>
</StackPanel>
System.Object
  System.Windows.Threading.DispatcherObject
    System.Windows.DependencyObject
      System.Windows.Media.Visual
        System.Windows.UIElement
          System.Windows.FrameworkElement
            System.Windows.Controls.Control
              System.Windows.Controls.ContentControl
                System.Windows.Controls.Primitives.ButtonBase
                  System.Windows.Controls.Primitives.ToggleButton
                    System.Windows.Controls.RadioButton
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0

Date

History

Reason

April 2009

Added an event handler implementation to the first example.

Customer feedback.

February 2009

Described how default styles change dependency properties.

Customer feedback.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.