Binding for a RadioButton group in XAML MVVM

4

If I add a RadioButton control and specify a property in the ViemModel , I can do the Binding normally, but if I have a list of objects and create a RadioButton for each object in the list, does it? how is it done so that the RadioButton selected makes Binding as SelectedItem ?

                                                                                                                                                                                                                                                                                                                                            

    
asked by Darío Alonso 06.01.2016 в 18:47
source

1 answer

3

Link with the information I found:

link

In the ViewModel I have a collection to generate the Items and an object for the SelectedItem

In the view, a ListBox to show the Items converted to RadioButton like this:

<ListBox ItemsSource="{Binding Continents}"
             SelectedItem="{Binding Selected}"
             BorderThickness="0"
             ScrollViewer.HorizontalScrollBarVisibility="Disabled"
             Background="Transparent">            
        <ListBox.ItemContainerStyle>

            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListBoxItem}">

                            <RadioButton Content="{Binding Name}"
                                         IsChecked="{Binding RelativeSource={
                                RelativeSource TemplatedParent}, Path=IsSelected}"/>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListBox.ItemContainerStyle>
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectionChanged">
                <ei:CallMethodAction TargetObject="{Binding}" MethodName="CBSelectionChanged"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </ListBox>

I hope it serves someone.

    
answered by 06.01.2016 / 21:49
source