locked
Dynamic RadioButton GroupName using databinding? RRS feed

  • Question

  • Hi Guys,

    Basically I needed to have the selection of a set of RadioButtons bound to the property of an object - and all inside a ItemsControl as well.
    To do this I used a little XAML I dug up which changed the control template for a listbox that is acted like a group of RadioButtons.
    The code for this is below:
    <Style x:Key="RadioButtonList" TargetType="{x:Type ListBox}">
                <Setter Property="BorderBrush" Value="{x:Null}" />
                <Setter Property="BorderThickness" Value="0" />
                <Setter Property="ItemsPanel">
                    <Setter.Value>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Horizontal"
                              VerticalAlignment="Center"
                              HorizontalAlignment="Center"/>
                        </ItemsPanelTemplate>
                    </Setter.Value>
                </Setter>
                <Setter Property="ItemContainerStyle">
                    <Setter.Value>
                        <Style TargetType="{x:Type ListBoxItem}" >
                            <Setter Property="Margin" Value="2" />
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type ListBoxItem}">
                                        <Border Background="Transparent">
                                            <RadioButton Focusable="False"
                                                IsHitTestVisible="False" Margin="5 0"
                                                IsChecked="{TemplateBinding IsSelected}">
                                                <ContentPresenter />
                                            </RadioButton>
                                        </Border>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </Setter.Value>
                </Setter>
            </Style>
    This would appear to work fine. However, I am now finding that  for every row in my ItemsControl I am getting the correct collection of radio buttons but they all seem to belong to the same group as I can only select one at a time. My question is, using the template above, how can I set the GroupName to something unique for each row of my outter ItemsControl (which is bound to an object pulled from a database)

    I know it would be easier to use a drop down and avoid these issues, but sadly I dont have a choice. :(

    Thanks in advance
    Sunday, May 17, 2009 7:46 PM

Answers

  • Hi There,
    Thanks for your reply.

    Dr WPF hit on the problem and solution in this thread.
    http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1e980f0b-a03e-473f-aec3-bb5406771040/

    Seems it was a problem with my selectedvalue databinding

    • Proposed as answer by Tao Liang Monday, May 18, 2009 2:28 AM
    • Marked as answer by Tao Liang Wednesday, May 20, 2009 5:18 AM
    Monday, May 18, 2009 12:06 AM

All replies

  • Let me retry this :)

    <

     

    RadioButton Focusable="False"

     

    IsHitTestVisible="False" Margin="5 0"

     

    IsChecked="{TemplateBinding IsSelected}"Ibrahim Malluf MCP http://www.malluf.com

    Sunday, May 17, 2009 11:17 PM
  • Hi There,
    Thanks for your reply.

    Dr WPF hit on the problem and solution in this thread.
    http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1e980f0b-a03e-473f-aec3-bb5406771040/

    Seems it was a problem with my selectedvalue databinding

    • Proposed as answer by Tao Liang Monday, May 18, 2009 2:28 AM
    • Marked as answer by Tao Liang Wednesday, May 20, 2009 5:18 AM
    Monday, May 18, 2009 12:06 AM
  • Hi There,
    Thanks for your reply.

    Dr WPF hit on the problem and solution in this thread.
    http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/1e980f0b-a03e-473f-aec3-bb5406771040/

    Seems it was a problem with my selectedvalue databinding

    So do you mean your problem have been solved by Dr WPF?
    Monday, May 18, 2009 2:28 AM
  • Yes, apologies for not marking it as the answer.
    Wednesday, May 20, 2009 11:00 PM