Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Get RadioButtons from RadioButtonGroups and vice versa

Get RadioButtons from RadioButtonGroups and vice versa

Scheduled Pinned Locked Moved WPF
csharpwpfcollaboration
13 Posts 5 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Defender NF

    Hey, I have in my application(wpf) an Indicator for all Input Boxes, wich indicate if the box is a mandatory field or not, and changes his color if the Field is filled. Now the product team would like to have this indicator even on radiobuttons, that means the indicator of the radiobutton should know if the others radiobuttons of the same group are filled or not to change the color to the right one. We have a new requiredradiobutton control wich derive from the Original Control: RadioButton, and i have to programm the Indicator of the control something like that:

    protected override void OnChecked(RoutedEventArgs e)

    {

    RadioButtonGroup myradiobuttonsgroup = this.GetRadioButtonGroup();

    foreach(RadionButton rb in myradiobuttonsgroup)

    {

    if rb.IsChecked != true

    rb.Indicator.Status = Status.NotFilled;

    else

    rb.Indicator.Status = Status.Filled;

    }

    }

    Or Another way how i can handle that without puting the radiobuttons in a listbox or something. Thanks a lot and Cheers. Negada

    M Offline
    M Offline
    Member 1033907
    wrote on last edited by
    #2

    The way I understand it, you want the indicator in the NotFilled state if none of the radio button within the group are checked and the Filled state if at least on of the radio button (within the same group) is checked. If this is correct, then your code is most likely wrong, this way it will set the indicator status based on the last radio button examined. Do something like

    rb.Indicator.Status = myradiobuttongroup.Any(p=>p.IsChecked) ? Status.Filled : Status.NotFilled;

    (not tested, may need polishing) H.

    D 1 Reply Last reply
    0
    • M Member 1033907

      The way I understand it, you want the indicator in the NotFilled state if none of the radio button within the group are checked and the Filled state if at least on of the radio button (within the same group) is checked. If this is correct, then your code is most likely wrong, this way it will set the indicator status based on the last radio button examined. Do something like

      rb.Indicator.Status = myradiobuttongroup.Any(p=>p.IsChecked) ? Status.Filled : Status.NotFilled;

      (not tested, may need polishing) H.

      D Offline
      D Offline
      Defender NF
      wrote on last edited by
      #3

      Hey, thanks for replying, yes u understand it right, i know the code was just an example.. what i really need is how to get the radiogroup of my radiobutton, and if i get the radiogroup how can i get all its radiobuttons.. cheers

      1 Reply Last reply
      0
      • D Defender NF

        Hey, I have in my application(wpf) an Indicator for all Input Boxes, wich indicate if the box is a mandatory field or not, and changes his color if the Field is filled. Now the product team would like to have this indicator even on radiobuttons, that means the indicator of the radiobutton should know if the others radiobuttons of the same group are filled or not to change the color to the right one. We have a new requiredradiobutton control wich derive from the Original Control: RadioButton, and i have to programm the Indicator of the control something like that:

        protected override void OnChecked(RoutedEventArgs e)

        {

        RadioButtonGroup myradiobuttonsgroup = this.GetRadioButtonGroup();

        foreach(RadionButton rb in myradiobuttonsgroup)

        {

        if rb.IsChecked != true

        rb.Indicator.Status = Status.NotFilled;

        else

        rb.Indicator.Status = Status.Filled;

        }

        }

        Or Another way how i can handle that without puting the radiobuttons in a listbox or something. Thanks a lot and Cheers. Negada

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #4

        There's no RadioButtonGroup in WPF/Silverlight AFAIK. Radio buttons are grouped by either using them in a single common parent element or using the RadioButton.GroupName property.

        Mark Salsbery :java:

        D 1 Reply Last reply
        0
        • M Mark Salsbery

          There's no RadioButtonGroup in WPF/Silverlight AFAIK. Radio buttons are grouped by either using them in a single common parent element or using the RadioButton.GroupName property.

          Mark Salsbery :java:

          D Offline
          D Offline
          Defender NF
          wrote on last edited by
          #5

          Yes that whats i mean, how can i get all radiobuttons of a panel/window with the same groupname?

          M P 2 Replies Last reply
          0
          • D Defender NF

            Yes that whats i mean, how can i get all radiobuttons of a panel/window with the same groupname?

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #6

            Can't you use databinding between the indicators and the Indicator.Status property? That would be the WPF way...

            Mark Salsbery :java:

            D 1 Reply Last reply
            0
            • M Mark Salsbery

              Can't you use databinding between the indicators and the Indicator.Status property? That would be the WPF way...

              Mark Salsbery :java:

              D Offline
              D Offline
              Defender NF
              wrote on last edited by
              #7

              Mark, i dont need for binding because i programm the logic in the control, anyway changing the indicator status isnt a problem.. what i need is how to find out all the radiobuttons if i have the groupname of one of them.

              M 1 Reply Last reply
              0
              • D Defender NF

                Mark, i dont need for binding because i programm the logic in the control, anyway changing the indicator status isnt a problem.. what i need is how to find out all the radiobuttons if i have the groupname of one of them.

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #8

                Iterate through the Panel.Children collection and check each elements type. If it's one of your radio buttons then check its GroupName property. VisualTreeHelper.GetChild[^] can help...

                Mark Salsbery :java:

                S 1 Reply Last reply
                0
                • D Defender NF

                  Yes that whats i mean, how can i get all radiobuttons of a panel/window with the same groupname?

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #9

                  Hoo boy, there's a big catch in sorting this that you need to be aware of. The Group Name is not a unique property. What do I mean by this? Well, suppose you create a usercontrol that consists of a group of radio buttons with a single group name, and then you apply that control in more than one location, then the group name would span ALL the radio buttons that contain this group name. This is a "by-design" feature of WPF, which is intended to allow you to spread your radio buttons across multiple containers.

                  Forgive your enemies - it messes with their heads

                  "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                  My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                  S D 2 Replies Last reply
                  0
                  • M Mark Salsbery

                    Iterate through the Panel.Children collection and check each elements type. If it's one of your radio buttons then check its GroupName property. VisualTreeHelper.GetChild[^] can help...

                    Mark Salsbery :java:

                    S Offline
                    S Offline
                    SledgeHammer01
                    wrote on last edited by
                    #10

                    Mark's method of iterating through all the children is the only public method. Lots of us use a class thats available on the internet called VisualTreeExtensions that does essentially the same thing, but wraps that code in an extension method for you. A GetVisualDecendants(); call would return all the radio buttons. You could expand that to include the group name. Pretty much the same thing as what Mark suggested though. If you are willing to use reflection, all that information has already been collected. RadioButton has a private member: private static Hashtable _groupNameToElements; thats a HashTable of ArrayLists using the group name as the key. Some purists may frown on accessing a base class's internal data since it can easily change, but honestly, in this case, I don't see that happening. I'd just derive a class from RadioButton and implement a method that accesses _groupNameToElements so in the slight 1% chance that Microsoft ever does change that, you only need to change one place. EDIT: See Petes post for a good point about my _groupNameToElements method :).

                    1 Reply Last reply
                    0
                    • P Pete OHanlon

                      Hoo boy, there's a big catch in sorting this that you need to be aware of. The Group Name is not a unique property. What do I mean by this? Well, suppose you create a usercontrol that consists of a group of radio buttons with a single group name, and then you apply that control in more than one location, then the group name would span ALL the radio buttons that contain this group name. This is a "by-design" feature of WPF, which is intended to allow you to spread your radio buttons across multiple containers.

                      Forgive your enemies - it messes with their heads

                      "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                      My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                      S Offline
                      S Offline
                      SledgeHammer01
                      wrote on last edited by
                      #11

                      Shouldn't really be an issue since you need to iterate through the visual tree to find all the radio buttons.

                      P 1 Reply Last reply
                      0
                      • S SledgeHammer01

                        Shouldn't really be an issue since you need to iterate through the visual tree to find all the radio buttons.

                        P Offline
                        P Offline
                        Pete OHanlon
                        wrote on last edited by
                        #12

                        It is if you start too high up the Visual Tree. In the example I talked about above, if you start at the root node, you will hit this problem.

                        Forgive your enemies - it messes with their heads

                        "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                        My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                        1 Reply Last reply
                        0
                        • P Pete OHanlon

                          Hoo boy, there's a big catch in sorting this that you need to be aware of. The Group Name is not a unique property. What do I mean by this? Well, suppose you create a usercontrol that consists of a group of radio buttons with a single group name, and then you apply that control in more than one location, then the group name would span ALL the radio buttons that contain this group name. This is a "by-design" feature of WPF, which is intended to allow you to spread your radio buttons across multiple containers.

                          Forgive your enemies - it messes with their heads

                          "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                          My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                          D Offline
                          D Offline
                          Defender NF
                          wrote on last edited by
                          #13

                          Thank you all for The Assistance Cheers

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups