Get RadioButtons from RadioButtonGroups and vice versa
-
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
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.
-
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.
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
-
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
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:
-
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:
Yes that whats i mean, how can i get all radiobuttons of a panel/window with the same groupname?
-
Yes that whats i mean, how can i get all radiobuttons of a panel/window with the same groupname?
Can't you use databinding between the indicators and the Indicator.Status property? That would be the WPF way...
Mark Salsbery :java:
-
Can't you use databinding between the indicators and the Indicator.Status property? That would be the WPF way...
Mark Salsbery :java:
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.
-
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.
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:
-
Yes that whats i mean, how can i get all radiobuttons of a panel/window with the same groupname?
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
-
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:
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 :).
-
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
Shouldn't really be an issue since you need to iterate through the visual tree to find all the radio buttons.
-
Shouldn't really be an issue since you need to iterate through the visual tree to find all the radio buttons.
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
-
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
Thank you all for The Assistance Cheers