none
Data-binding to a radio button group RRS feed

  • Question

  • Since RadioButtonList is now obsolete in the CTP release, what is the recommended method for data-binding to a group of radio-buttons?  I don't want to data-bind each individual radio button.
    Tuesday, November 1, 2005 1:19 AM

Answers

  • Here's a sample

    <ListBox KeyboardNavigation.DirectionalNavigation="Cycle" BorderBrush = "Transparent"  Name="rbl1">

            <ListBox.Resources>

                <Style x:Key="{x:Type ListBoxItem}" TargetType="{x:Type ListBoxItem}" >

                    <Setter Property="Margin" Value="2, 2, 2, 0" />

                    <Setter Property="Template">

                    <Setter.Value>

                        <ControlTemplate>

                        <Border Background="Transparent">

                              <RadioButton IsHitTestVisible="false"     Focusable="false" Content="{TemplateBinding ContentPresenter.Content}"  IsChecked="{Binding Path=IsSelected,RelativeSource=/TemplatedParent,Mode=TwoWay}"/>

                        </Border>

                        </ControlTemplate>

                    </Setter.Value>

                    </Setter>

                </Style>

            </ListBox.Resources>       

                <ListBoxItem>RadioButton1</ListBoxItem>

                <ListBoxItem>RadioButton2</ListBoxItem>

                <ListBoxItem>RadioButton3</ListBoxItem>

          </ListBox

     Thanks

    Namita Gupta [MSFT]

    Tuesday, November 1, 2005 9:32 PM

All replies

  • A radio button list is now achieved by styling a single-select ListBox control such that each ListBoxItem is a RadioButton.  An example can be found in the ExpenseIt Demo in the WinFX SDK.

    Using this new approach, you would bind to the ListBox.

    Cheers,
    -Jonathan
    Tuesday, November 1, 2005 7:44 PM
  • Here's a sample

    <ListBox KeyboardNavigation.DirectionalNavigation="Cycle" BorderBrush = "Transparent"  Name="rbl1">

            <ListBox.Resources>

                <Style x:Key="{x:Type ListBoxItem}" TargetType="{x:Type ListBoxItem}" >

                    <Setter Property="Margin" Value="2, 2, 2, 0" />

                    <Setter Property="Template">

                    <Setter.Value>

                        <ControlTemplate>

                        <Border Background="Transparent">

                              <RadioButton IsHitTestVisible="false"     Focusable="false" Content="{TemplateBinding ContentPresenter.Content}"  IsChecked="{Binding Path=IsSelected,RelativeSource=/TemplatedParent,Mode=TwoWay}"/>

                        </Border>

                        </ControlTemplate>

                    </Setter.Value>

                    </Setter>

                </Style>

            </ListBox.Resources>       

                <ListBoxItem>RadioButton1</ListBoxItem>

                <ListBoxItem>RadioButton2</ListBoxItem>

                <ListBoxItem>RadioButton3</ListBoxItem>

          </ListBox

     Thanks

    Namita Gupta [MSFT]

    Tuesday, November 1, 2005 9:32 PM
  • I see how the radio button is tied to the currently selected listbox item.  But how do I ensure that the listbox is initialized such that the appropriate item is selected.  i.e. How do I data bind the listbox IsSelected property?
    Wednesday, January 4, 2006 8:21 AM
  • I'm on .NET 3.0 (VS2008-B2) and had to change the RadioButton inside the ControlTemplate (child of Border). I think a relative source cannot be declared like that (I think ;-)

     

    <RadioButton IsHitTestVisible="false" Focusable="false"

    Content="{TemplateBinding ContentPresenter.Content}"

    IsChecked="{Binding Path=IsSelected,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}"

    />

    • Proposed as answer by Jozef Izso Monday, March 7, 2011 6:14 PM
    Sunday, August 12, 2007 1:10 PM
  • Can this be used if I have a collection of  items, and dynamically create radio buttons based on the number of items in the collection ?

    Tuesday, October 30, 2007 3:09 PM
  • Hello,
    thank you for your code. But it doesn't work to me, when I bind my data from code behind.. Result is RadioButtonList items with text from ToString() method of binded objects (such as Test.Business.Person = DisplayMemberPath property is not used..). How can I change template of ListBox item to use DisplayMemberPath of ListBox?

    xaml:
    <Style Name="RadioButtonList">
    ... similiar as your code..

    <ListBox Name="rbl1" Style="{DynamicSource RadioButtonList}" DisplayMemberPath="Name" />

    code-behind:
    List<Person> p = new
    List<Person>(){new Person(){Name="Jack"}, new Person(){Name="M"}};
    rbl1.ItemsSource = p;

    Any help will be appreciated..
    vp
    Wednesday, March 26, 2008 10:37 AM
  •  I have the same issue.  Have you found a reasonable solution for this?
    Thursday, July 31, 2008 6:04 PM
  • A possible way is to put your datatemple code into the <RadioButton> such as :
                  <RadioButton GroupName="AreaLayoutGroup" 
                                        IsChecked="{Binding Path=IsSelected,RelativeSource={RelativeSource TemplatedParent}}">  
                    <Image Source="{Binding YourImagePath}" /> 
                    </RadioButton> 


    You can replace <Image Source> with your code!
    Saturday, August 2, 2008 10:54 AM
  • Change the binding on the Content of your RadioButton to point at your desired DisplayMemberPath:

    <RadioButton
         IsHitTestVisible="false"     
         Focusable="false"
         Content="{Binding Name}"
         IsChecked="{Binding Path=IsSelected, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
         />
    Thursday, August 13, 2009 7:21 PM