Ask a questionAsk a question
 

AnswerData Binding Radio Button Group

  • Tuesday, March 18, 2008 2:22 PMpremiumpixel Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hello!

    I'm having a small problem here with data binding to a Radio Button Group.

    I want to get the "Content" Value of the selected Radio Button. How do I connect to that?

    {Binding Path=Content, ElementName=radioButton0, Mode=Default}

     

    This seems to work for one RadioButton, but how do I get the value of the SelectedItem of the whole RadioButton group?


    Here's my code:

    Code Snippet

     <Canvas>
      <StackPanel x:Name="testPanel">
       <RadioButton Content="RadioButton 0" GroupName="testGroup" x:Name="radioButton0" />
       <RadioButton Content="RadioButton 1" GroupName="testGroup" x:Name="radioButton1" IsChecked="True"  />
      </StackPanel>
      <TextBox x:Name="textBox" Canvas.Top="40" Canvas.Left="0" Width="100" Text="{Binding Path=Content, ElementName=radioButton0, Mode=Default}"></TextBox>
     </Canvas>

     

     

    Thanks for your help in advance!

Answers

  • Thursday, March 20, 2008 4:32 AMWei Zhou - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    You can use a ListBox to group the RadioButtons, and bind the ListViewItem.IsSelected property to RadioButton.IsChecked property. Setting the TextBox.Text property to ListBox.SelectedItem property. Here is the example.

     

    Code Snippet

    <StackPanel>

        <ListBox x:Name="RadioButtonList" BorderThickness="0" Background="{x:Null}">

            <ListBox.ItemContainerStyle>

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

                    <Setter Property="Template">

                        <Setter.Value>

                            <ControlTemplate TargetType="{x:Type ListBoxItem}">

                                <ContentPresenter/>

                            </ControlTemplate>

                        </Setter.Value>

                    </Setter>

                </Style>

            </ListBox.ItemContainerStyle>

     

            <ListBoxItem IsSelected="{Binding IsChecked, ElementName=RadioButton1, Mode=OneWay}">

                <RadioButton x:Name="RadioButton1" GroupName="Group">RadioButton1</RadioButton>

            </ListBoxItem>

            <ListBoxItem IsSelected="{Binding IsChecked, ElementName=RadioButton2, Mode=OneWay}">

                <RadioButton x:Name="RadioButton2" GroupName="Group">RadioButton2</RadioButton>

            </ListBoxItem>

        </ListBox>

        <TextBox Text="{Binding SelectedItem.Content.Content, ElementName=RadioButtonList}"/>

    </StackPanel>

     

     

    Best Regards,

    Wei Zhou

All Replies

  • Thursday, March 20, 2008 4:32 AMWei Zhou - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    You can use a ListBox to group the RadioButtons, and bind the ListViewItem.IsSelected property to RadioButton.IsChecked property. Setting the TextBox.Text property to ListBox.SelectedItem property. Here is the example.

     

    Code Snippet

    <StackPanel>

        <ListBox x:Name="RadioButtonList" BorderThickness="0" Background="{x:Null}">

            <ListBox.ItemContainerStyle>

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

                    <Setter Property="Template">

                        <Setter.Value>

                            <ControlTemplate TargetType="{x:Type ListBoxItem}">

                                <ContentPresenter/>

                            </ControlTemplate>

                        </Setter.Value>

                    </Setter>

                </Style>

            </ListBox.ItemContainerStyle>

     

            <ListBoxItem IsSelected="{Binding IsChecked, ElementName=RadioButton1, Mode=OneWay}">

                <RadioButton x:Name="RadioButton1" GroupName="Group">RadioButton1</RadioButton>

            </ListBoxItem>

            <ListBoxItem IsSelected="{Binding IsChecked, ElementName=RadioButton2, Mode=OneWay}">

                <RadioButton x:Name="RadioButton2" GroupName="Group">RadioButton2</RadioButton>

            </ListBoxItem>

        </ListBox>

        <TextBox Text="{Binding SelectedItem.Content.Content, ElementName=RadioButtonList}"/>

    </StackPanel>

     

     

    Best Regards,

    Wei Zhou

  • Thursday, March 20, 2008 1:36 PMpremiumpixel Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

     

    Thanks a lot Wei. That did the trick! Cool! Smile
  • Thursday, July 10, 2008 3:08 PMAlex S-D Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    This is just what I needed... with a slight difference...

    How can you get the radio buttons to be side-by-side?
  • Monday, April 13, 2009 5:26 AMzepher684 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi,
    This helped me probably it might prove usefull for you too.


    <

    Style x:Key="VerticalRadioButtonListStyle" TargetType="ListBox">

     

    <Style.Resources>

     

    <Style TargetType="ListBoxItem">

     

    <Setter Property="Template">

     

    <Setter.Value>

     

    <ControlTemplate TargetType="ListBoxItem">

     

    <Grid>

     

    <Grid.ColumnDefinitions>

     

    <ColumnDefinition Width="Auto" />

     

    </Grid.ColumnDefinitions>

     

    <RadioButton IsChecked="{Binding IsSelected,

     

    RelativeSource={RelativeSource TemplatedParent},

     

    Mode=TwoWay}" Content="{Binding Content,

     

    RelativeSource={RelativeSource TemplatedParent},

     

    Mode=TwoWay}"/>

     

    </Grid>

     

    </ControlTemplate>

     

    </Setter.Value>

     

    </Setter>

     

    </Style>

     

    </Style.Resources>

     

    <Setter Property="ItemsPanel">

     

    <Setter.Value>

     

    <ItemsPanelTemplate>

     

    <WrapPanel Orientation="Horizontal" />

     

    </ItemsPanelTemplate>

     

    </Setter.Value>

     

    </Setter>

     

    <Setter Property="BorderThickness" Value="0" />

     

    <Setter Property="Background" Value="Transparent" />

     

    </Style>

    Zepher


    Zepher684