【问题标题】:Multiple selection with checkbox using Datagrid in WPF在 WPF 中使用 Datagrid 的多选复选框
【发布时间】:2013-11-01 05:42:03
【问题描述】:

我想知道如何使用 DataGridCheckBoxColumn 选择多行。

这里我只能选择一行,但是如何进行多选。

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

谢谢

序列号

【问题讨论】:

    标签: c# wpf mvvm checkbox datagrid


    【解决方案1】:

    你可以做到,感谢 Scott

    https://blog.scottlogic.com/2008/11/26/multiselect-datagrid-with-checkboxes.html

    诀窍是使用如下所示的 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>
    

    【讨论】:

      【解决方案2】:

      SelectionMode="Extended"SelectionMode="Multiple" 将使您的 DataGrid 多选

      【讨论】:

      • 您无法在 DataGrid 中选择多行?
      • 是的,只能选择一行!!当我选择第二个时,已选择的项目未选中:(
      • 这很奇怪.. 你是如何尝试选择行的.. 通过拖动单击并将鼠标拖到行上?
      • 我尝试通过单击复选框进行选择。单击下一行项目时,已选中的项目未选中。
      • 当我使用 Shift 键选择行时,可以进行多选。不是一一对应。
      猜你喜欢
      • 2018-06-24
      • 2016-01-13
      • 1970-01-01
      • 2018-08-03
      • 2017-04-24
      • 1970-01-01
      • 2013-06-11
      • 1970-01-01
      • 2013-01-16
      相关资源
      最近更新 更多