在 WPF 中使用 Datagrid 使用复选框进行多项选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19533356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple selection with checkbox using Datagrid in WPF
提问by SNS
I would like to know how to select multiple rows with DataGridCheckBoxColumn .
我想知道如何使用 DataGridCheckBoxColumn 选择多行。
Here I'm able to select only one row, but how to do multiple selection.
在这里我只能选择一行,但是如何进行多选。
My XAML is as follows:
我的 XAML 如下:
<UserControl.Resources>
<Style x:Key="itemstyle" TargetType="{x:Type DataGridRow}">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightGoldenrodYellow" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Black" />
</Style.Resources>
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}" />
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="ItemsControl.AlternationIndex" Value="1" />
<Condition Property="IsSelected" Value="False" />
<Condition Property="IsMouseOver" Value="False" />
</MultiTrigger.Conditions>
<Setter Property="Background" Value="#EEEEEEEE" />
</MultiTrigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
<Grid Width="500" Height ="300">
<DataGrid ItemsSource="{Binding Path=Script}" HeadersVisibility="Column" SelectionMode="Single" AlternatingRowBackground="Gainsboro" Background="White" AutoGenerateColumns="False" ItemContainerStyle="{StaticResource itemstyle}" CanUserAddRows="True" GridLinesVisibility="None" Height="242" HorizontalAlignment="Left" HorizontalContentAlignment="Left" IsEnabled="True" IsReadOnly="True" Margin="10,14,0,44" Name="dgMain" RowHeight="23" VerticalAlignment="Center" VerticalContentAlignment="Center" Width="478" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<i:InvokeCommandAction Command="{Binding EditData}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<DataGrid.Columns>
<DataGridCheckBoxColumn Binding="{Binding Path=IsSelected}" Header="Select" Width="50" />
<DataGridTextColumn Binding="{Binding Path=Script_Text}" Header="Script" Width="400" />
</DataGrid.Columns>
</DataGrid>
</Grid>
thanks
谢谢
SN
SN
采纳答案by Nitin
SelectionMode="Extended"or SelectionMode="Multiple"will make your DataGrid multiselect
SelectionMode="Extended"或SelectionMode="Multiple"将使您的 DataGrid 多选
回答by IronHide
You can do it, credit goes to Scott
你可以做到,归功于斯科特
https://blog.scottlogic.com/2008/11/26/multiselect-datagrid-with-checkboxes.html
https://blog.scottlogic.com/2008/11/26/multiselect-datagrid-with-checkboxes.html
And the trick is to use RowHeaderTemplate like below
诀窍是使用如下所示的 RowHeaderTemplate
<DataGrid ItemsSource="{Binding}">
<DataGrid.RowHeaderTemplate>
<DataTemplate>
<Grid>
<CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type dg:DataGridRow}}}"/>
</Grid>
</DataTemplate>

