The WPF RadioButton

Being an old VB coder, I fondly remember this control as the Option Button, but these days it’s been renamed to the RadioButton. They are used to present the user with a set of mutually exclusive options.

Adding them is simple, just use the <RadioButton> tag. For the button you wish to be the default (if any) you add the IsChecked=”true” flag.

WPF adds a huge improvement over it’s predecessor. No longer are you forced to use a container control to group your radio buttons. Instead, WPF adds a GroupName property. Take a look at this example:  

  <StackPanel>

    <RadioButton GroupName=One IsChecked=True>Option 1</RadioButton>

    <RadioButton GroupName=One IsChecked=False>Option 2</RadioButton>

    <RadioButton GroupName=Two IsChecked=False>Option 3</RadioButton>

    <RadioButton GroupName=Two IsChecked=True>Option 4</RadioButton>

  </StackPanel>

wpf045

Note the GroupName, for the first two items, I’ve set it to One, on the second two it’s Two. This means you can change Option 1 and Option 2 without affection Options 3 or 4. Go ahead and run the app, click on the options a bit and watch what happens.

In my example, I only entered two items for each, the actual number you can do is limited only by the space you have available on your Window.

The C# code for checking the value is identical to yesterday’s code for the CheckBox so I won’t reiterate it, all you have to do is look at the IsChecked property.

One last point, event though adding containers are not required to separate the option groups, it’s important to provide some sort of visual indicator so the user knows which groups do what.

About these ads

12 Responses to “The WPF RadioButton”

  1. Carlos de Luna Sáenz Says:

    In your example… how do i know wich one is selected by the user?
    Greetings

  2. Carlos de Luna Sáenz Says:

    What i meant is… i have to “iterate” or “look” on each one of the radio Buttons… how if you didn’t use the Name property… can i use the groupName and get the radioButtons inside the group???
    Thanks

  3. Mark Heydenrych Says:

    Fantastic little tutorial. Quick, helpful, clear. I’ve found lots of useful stuff on this site. Thanks ;)

  4. John Q. Public Says:

    Excellent article. Here is something just to complete the loop.

    Given this XAML:
    ///

    Option 1
    Option 2
    Option 3

    /// and this code behind (C#)

    bool? testWhichRadioButtonClickedBool = null;

    testWhichRadioButtonClickedBool = RadioBtn1.IsChecked;

    if (testWhichRadioButtonClickedBool != null)
    {
    if (testWhichRadioButtonClickedBool != false)
    {
    //do first procedure here

    }
    }

    testWhichRadioButtonClickedBool = RadioBtn2.IsChecked;

    if (testWhichRadioButtonClickedBool != null)
    {
    if (testWhichRadioButtonClickedBool != false)
    {
    //do second procedure here

    }
    }

    testWhichRadioButtonClickedBool = RadioBtn3.IsChecked;

    if (testWhichRadioButtonClickedBool != null)
    {
    if (testWhichRadioButtonClickedBool != false)
    {
    //do third procedure here

    }
    }

    //only one of these three if statements is true at any time

  5. http://www.thetreespecialists.com/ Says:

    com provides information on Fishing, Fly Fishing, Fishing Boats, Deep Sea Fishing and.
    Pay close attention to the mayflies, midges, and stoneflies in the
    area and try to match your flies to the size and type fish are biting on.
    But here’s my top 5 to consider with choices for both types of water included:.

  6. tmz twitter aol tmz online Says:

    Where there was less effort wasted, so is less time wasted.
    It is also possible for users to conduct charitable and humanitarian
    works online. – Its also a great idea to insert a bio
    about you, people do want to know who you are and what you like, remember this is social networking.

  7. Alisha Says:

    This is more important than ever in a world
    where there are dozens of different audio formats.
    They also apparently believe that their customers who like a particular song they hear on
    the radio, are likely to purchase that song, which could add to downloads from i – Tunes.
    Whats more is that 2G phones can come in a tinier and slimmer package, even its batteries.

  8. Andrew Says:

    If, at the end of this time period, the score is
    tied, there are two options the officials can take.
    By obtaining increasingly more favour using their company
    people in your social settings, the more handle you will need to acquire what you would like, and thru
    The particular Coalition Game you have just about the most effective tips for hypnotherapy that gives you the
    talent to ensure that you influence other folks. This classic game integrates all-out entertainment with vocabulary enhancement in one amazing game.

  9. Internet Radio Says:

    The Squeezbox Boom radio from Logitech is not just an Internet radio.
    “It’s hard to believe, ” says Randy Gilbert, host of “The Inside Success Secrets Radio Show”-an Internet radio broadcast which has been “airing”
    for several years. They explained how banners are
    bought via a network so they get low CPMs, an obscene amount of views and a good click-through ratio.

  10. [主页] Says:

    The developers are saying that Defiance is a pixel perfect shooter,
    so if you aim at your target’s head it WILL be a headshot instead of the invisible dice rolling a 5 and telling you that you somehow missed. It should come as no surprise that i – Tunes is loaded full of pirate apps for i – Phone, i – Pod Touch, and i – Pad. Preparing to apply to the dozens of other game development companies for video game jobs will enable you to expand and diversify your list of possible employers, and your chances of getting hired will increase tremendously.

  11. Marcela Says:

    Dominion: Intrigue is a standalone expansion for the award-winning Dominion card game that
    shook the gaming world in 2008. Caution: when a hole is made in a can, there may be metallic edges that can cut one’s mouth. This classic game integrates all-out entertainment with vocabulary enhancement in one amazing game.

  12. Rickie Says:

    Though the levels look really simple, they are actually quite challenging.
    Just last year Apple sold more than one hundred million i – Phone and i – Pad apps.
    Most video game testers reported that their salary falls between $15,000 and $55, 000 in a year.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 84 other followers

%d bloggers like this: