1

My datagrid is bound to an observable collection, The datagrid has two columns - one for radio buttons, another to display names. At the moment, I am able to make multiple selections on the datagrid, But my requirement is to select only one radio button at a time such that the selected radio button should notify the viewmodel about the selected user name. How can i achieve it ?

Here's what I tried:

<DataGrid Width="{Binding ActualWidth, ElementName=panel}" 
                              ItemsSource="{Binding obvUsers}"                           
                              Height="390" >                                                                                                                                                                        
                        <DataGrid.Columns>                            
                            <DataGridTemplateColumn Header="" Width="100">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <RadioButton IsChecked="{Binding IsUserSelected}"                                    
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                            <DataGridTemplateColumn Header="Name">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding UserName}" />
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>                
                        </DataGrid.Columns>                       
                    </DataGrid>
DotNetSpartan
  • 931
  • 4
  • 20
  • 41
  • Possible duplicate of [How to get a group of toggle buttons to act like radio buttons in WPF?](https://stackoverflow.com/questions/2362641/how-to-get-a-group-of-toggle-buttons-to-act-like-radio-buttons-in-wpf) – Sasha Jul 09 '18 at 15:07

1 Answers1

2

Just put a GroupName for your RadioButton. All RadioButton in DataGrid should have a single selection because they have the same group. This works for me.

<DataGridTemplateColumn Header="" Width="100">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <RadioButton GroupName="abc" IsChecked="{Binding IsUserSelected}">
            </RadioButton>                                    
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
DanTheMan
  • 3,277
  • 2
  • 21
  • 40
Antoine V
  • 6,998
  • 2
  • 11
  • 34