0

I have a datagrid and my purpose is to select multiple rows when the checbox cell of the row is checked. Problem is that one row is selected always. IsSelected property of the previous checked rows return false if i select one row. Please check the code behind. When checkbox of any row is checked, i set the IsSelected property to true. Next check of other rows remove the previous selections. I don't know the reason.

Xaml

<DataGrid VirtualizingStackPanel.VirtualizationMode="Standard"  EnableColumnVirtualization = "True" EnableRowVirtualization ="True" x:Name="deckGrid" Grid.Row="3" ItemsSource="{Binding DeckList}" AutoGenerateColumns="False" 
            SelectionMode="Extended" Margin="10,10,0,0" SelectionUnit="FullRow" SelectionChanged="deckGrid_SelectionChanged">

 <DataGrid.Columns >
                <DataGridTemplateColumn Width="70" >
                    <DataGridTemplateColumn.HeaderTemplate >
                        <DataTemplate>
                            <CheckBox  Name="ckbSelectedAll" IsThreeState="True" Margin="10,0,0,0"  IsChecked="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:DeckDefinition}, Path=AllSelected}">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="Unchecked">
                                        <i:InvokeCommandAction Command="{x:Static local:DeckDefinition.UnCheckedCommand}"/>
                                    </i:EventTrigger>
                                    <i:EventTrigger EventName="Checked">
                                        <i:InvokeCommandAction Command="{x:Static local:DeckDefinition.CheckedCommand}"/>
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </CheckBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.HeaderTemplate>
                    <DataGridTemplateColumn.CellTemplate >
                        <DataTemplate>
                            <CheckBox IsChecked="{Binding IsChecked, UpdateSourceTrigger=PropertyChanged}"  Name="cbkSelect" Margin="10,0,0,0"  Checked="CheckedTest"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Header="{lex:LocText Key=name, Dict=general, Assembly=KillCardMain}" Binding="{Binding Path=Name}" ></DataGridTextColumn>
                <DataGridTemplateColumn HeaderStyle="{StaticResource CollapsedHeaderStyle}" CellStyle="{StaticResource NoBackgroundCellStyle}">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button Width="32" Style="{DynamicResource NoBackgroundButtonStyle}" Click="DeleteRowBtnClicked">
                                <Image Source="/KillCardMain;component/Resources/Images/delete.png" Width="16"></Image>
                            </Button>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn HeaderStyle="{StaticResource CollapsedHeaderStyle}" CellStyle="{StaticResource NoBackgroundCellStyle}">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button Width="32" Style="{DynamicResource NoBackgroundButtonStyle}">
                                <Image Source="/KillCardMain;component/Resources/Images/edit.png" Width="16"></Image>
                            </Button>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

Code behind

private void CheckedTest(object sender, RoutedEventArgs e) {
    int index = deckGrid.SelectedIndex;
    DataGridRow row = (DataGridRow)deckGrid.ItemContainerGenerator.ContainerFromIndex(index);
    row.IsSelected = true;

        }

enter image description here

nihasmata
  • 652
  • 1
  • 8
  • 28

1 Answers1

0

The standard selection functionality of DataGrid is to select only the new row whenever a row is clicked, unless Ctrl or Shift is pressed. It sounds like you want the selection process to be controlled only by whether those checkboxes are checked, but then you will have to somehow disable all the normal selection behavior of DataGrid.

M Kloster
  • 679
  • 4
  • 13