2016-06-22 93 views
0

我在wpf工作,我使用下面的DataGrid:多選在數據網格禁用WPF

<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> 

但還是我能選擇多的複選框。如何禁用此功能。 (我想只選擇一個複選框,在選擇下一個複選框時,前一個應該被取消選中)

回答

0

嘗試RadioButton而不是CheckBox。

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

,如果你願意,你可以自定義單選按鈕的模板。

<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>