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

    Question

  • 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?

    Thanks

    NIK

     

     

     

     

     

     

    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?

    Adamus

    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.

    Thanks

     

    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>

    </DataTemplate>

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

                         <ItemsControl.ItemsPanel>

                               <ItemsPanelTemplate>

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

                               </ItemsPanelTemplate>

                         </ItemsControl.ItemsPanel>

                         <ItemsControl.ItemsSource>                     

                                      <s:ArrayList>

                                             <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>

                                      </s:ArrayList>                   

                         </ItemsControl.ItemsSource>

                  </ItemsControl>

    void Clicked(object sender, RoutedEventArgs e)

    {

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

    }

    Tuesday, October 10, 2006 2:12 PM
    Moderator
  • 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

    xmlns:p="clr-namespace:System"

    xmlns:s="clr-namespace:System.Collections"

    -----

    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"

    xmlns:p="clr-namespace:System;assembly=mscorlib"

    Tuesday, October 10, 2006 6:20 PM
    Moderator
  • 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