简体   繁体   中英

Multiple Selection disable in datagrid in wpf

I'm working in wpf and I am using below data-grid:

<DataGrid Name="dgUseCaseList" AutoGenerateColumns="False" Grid.Row="0" CanUserAddRows="False" VirtualizingStackPanel.IsVirtualizing="False" Height="620" VerticalAlignment="Top" SelectionMode="Single" >
                        <DataGrid.Columns>
                            <DataGridTemplateColumn Width="30">
                                <DataGridTemplateColumn.Header>
                                    <CheckBox Name="chkSelectAllUseCases" Click="chkSelectAllUseCases_Click" IsHitTestVisible="False"/>
                                </DataGridTemplateColumn.Header>
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <CheckBox Name="chkSelectUseCase" IsChecked="{Binding Path=IsSelected,Mode=TwoWay}" Click="chkSelectUseCase_Click"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>

                            <DataGridTextColumn Width="100" Header="UseCaseId"   Binding="{Binding Path=UseCaseId}">
                                <DataGridTextColumn.CellStyle>
                                    <Style TargetType="DataGridCell">
                                        <Setter Property="ToolTip" Value="{Binding UseCaseDescription}" />
                                    </Style>
                                </DataGridTextColumn.CellStyle>
                            </DataGridTextColumn>
                        </DataGrid.Columns>
                    </DataGrid>

But still I am able to select multiple check-boxes. How to disable this. (I want to select just one checkbox. On selection of next checkbox,the previous one should get unchecked)

Try RadioButton instead of CheckBox.

<DataGridTemplateColumn.CellTemplate> <DataTemplate> <RadioButton GroupName="group1" Name="chkSelectUseCase" IsChecked="{Binding Path=IsSelected,Mode=TwoWay}" Click="chkSelectUseCase_Click"/> </DataTemplate> </DataGridTemplateColumn.CellTemplate>

You can customize RadioButton's Template if you want.

<Style TargetType="{x:Type RadioButton}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type RadioButton}"> <Grid> <CheckBox IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsChecked, Mode=TwoWay}" IsHitTestVisible="False" Content="{TemplateBinding Content}" /> <CheckBox IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsChecked, Mode=TwoWay}"
Content="{TemplateBinding Content}" Opacity="0"/> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM