Data Binding: How to bind a group of Radio Buttons to a value/collection?


  • Are there any properties that represent the selected state of a group of Radio Buttons?

    Seems that Radio Buttons are very different controls from others in that cannot represent any value in and of themselves, they must be combined with others.

    Thus one typically wants to bind the entire group to a single value and map the selected radio button in some way to represent a value. In the most simple case 2 radio buttons representing to a collection of {true,false}.

    Is this the kind of situation where MultiBindings might be applied?









    Monday, October 09, 2006 9:07 PM

All replies

  • Each control in a RadioButtonList does indeed have a unique value.

    If you're asking how to bind it to a datasource...ofcourse. Is this what your asking?

    Or are you referring to the selectedindexchanged event?


    Monday, October 09, 2006 9:15 PM
  • Trying to figure out how to bind a group of buttons to anm underlying collection. Looks like RadioButtonList is the kind of thing I am after, but the docs suggest that it is ASP only. Is there a "fat client" equivalent?

    Please advise.



    Tuesday, October 10, 2006 1:56 PM
  • here is a sample, radiobutton is created with groupname property set, so we get a Radiobuttonlist

    <DataTemplate x:Key="dt">

    <RadioButton GroupName="group1" Tag="{Binding Path=Year}" Content="{Binding}"></RadioButton>


    <ItemsControl RadioButton.Click="Clicked" Name="ctrl" ItemTemplate="{StaticResource dt}" >



                                      <StackPanel IsItemsHost="True"></StackPanel>





                                             <p:DateTime>1993/1/1 12:22:02</p:DateTime>

                                             <p:DateTime>1993/1/2 13:2:01</p:DateTime>

                                             <p:DateTime>1997/1/3 2:1:6</p:DateTime>

                                             <p:DateTime>1997/1/4 13:6:55</p:DateTime>

                                             <p:DateTime>1999/2/1 12:22:02</p:DateTime>

                                             <p:DateTime>1998/2/2 13:2:01</p:DateTime>

                                             <p:DateTime>2000/2/3 2:1:6</p:DateTime>

                                             <p:DateTime>2002/2/4 13:6:55</p:DateTime>

                                             <p:DateTime>2001/3/1 12:22:02</p:DateTime>

                                             <p:DateTime>2006/3/2 13:2:01</p:DateTime>

                                             <p:DateTime>2004/3/3 2:1:6</p:DateTime>

                                             <p:DateTime>2004/3/4 13:6:55</p:DateTime>




    void Clicked(object sender, RoutedEventArgs e)


    MessageBox.Show((e.OriginalSource as RadioButton).Tag.ToString());


    Tuesday, October 10, 2006 2:12 PM
  • I need to bind to a business object, well outside of code behind, but this gives me some of the picture.

    However, how/where are s: and p: declared?

    I tried this, and received a compile error on ArrayList




    Error 3 The tag 'ArrayList' does not exist in XML namespace 'clr-namespace:System.Collections'.




    Tuesday, October 10, 2006 6:15 PM
  • xmlns:s="clr-namespace:System.Collections;assembly=mscorlib"


    Tuesday, October 10, 2006 6:20 PM
  • Thanks Lee, that got the sample up and running.

    I was able to adapt your sample to work with an external (to the window class) object that exposes a collection.

    The one piece that is missing, is being able to two-way bind the selected radio button to a "SelectedItem".  I could repurpose the Tag object to store the name of the selected item in my underlying object, but I'm not clear on how I might automatically bind the IsChecked value of the appropriate radio button.

    Note: I have a mandate not to use code-behind.





    Tuesday, October 10, 2006 8:06 PM