本文介绍了在WPF中使用Datagrid选中复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



在这里,我只能选择一行,但是如何选择多项。 / p>

我的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 = Horizo​​ntalContentAlignment Value = Stretch />
< Setter Property = IsSelected Value = {Binding Path = IsSelected,Mode = TwoWay} />
< Style.Triggers>
< MultiTrigger>
< MultiTrigger.Conditions>
<条件属性= ItemsControl.AlternationIndex值= 1 />
<条件属性= IsSelected值= False />
<条件属性= IsMouseOver值= False />
< /MultiTrigger.Conditions>
< Setter Property = Background Value =#EEEEEEEE />
< / MultiTrigger>
< /Style.Triggers>
< / Style>
< /UserControl.Resources>

<网格宽度= 500高度= 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 Horizo​​ntalAlignment = Left Horizo​​ntalContentAlignment = 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}标题=选择 Width = 50 />
< DataGridTextColumn Binding = {Binding Path = Script_Text}标题= Script宽度= 400 />
< /DataGrid.Columns>
< / DataGrid>
< / Grid>

谢谢



SN

解决方案

SelectionMode =扩展 SelectionMode = Multiple 将使您的DataGrid多选


I would like to know how to select multiple rows with DataGridCheckBoxColumn .

Here I'm able to select only one row, but how to do multiple selection.

My XAML is as follows:

<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

解决方案

SelectionMode="Extended" or SelectionMode="Multiple" will make your DataGrid multiselect

这篇关于在WPF中使用Datagrid选中复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 20:22