Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
440
Selected items in XamDataGrid
posted

Hello everyone!

I have a XamDataGrid which binds to a collection of custom objects, let's call it "FooItem". These objects has a property IsSelected.

With inspiration from Josh Smith's blogpost

(http://blogs.infragistics.com/blogs/josh_smith/archive/2008/09/04/adding-checkboxes-to-the-record-selectors-in-xamdatagrid.aspx)

, I have styled the RecordSelector and the HeaderPrefixArea as follows:

<Style TargetType="{x:Type DataPresenter:RecordSelector}">
         <Setter Property="Template">
            <Setter.Value>
               <ControlTemplate TargetType="{x:Type DataPresenter:RecordSelector}">
                  <CheckBox
                     x:Name="RecordSelectorCheck"
                     HorizontalAlignment="Center"
                     VerticalAlignment="Center"
                     IsChecked="{Binding Path=DataItem.IsSelected, UpdateSourceTrigger=PropertyChanged}"/>
               </ControlTemplate>
            </Setter.Value>
         </Setter>
      </Style>


     

<Style TargetType="{x:Type DataPresenter:HeaderPrefixArea}">
         <Setter Property="Visibility" Value="Visible"/>
         <Setter Property="Template">
            <Setter.Value>
               <ControlTemplate TargetType="{x:Type DataPresenter:HeaderPrefixArea}">
                  <CheckBox
                     HorizontalAlignment="Center"
                     VerticalAlignment="Center"
                     IsChecked="{Binding Path=DataPresenter.DataContext.IsAllItemsSelected, UpdateSourceTrigger=PropertyChanged}"/>
               </ControlTemplate>
            </Setter.Value>
         </Setter>
      </Style>

 

In my code-behind i have this eventhandler, which updates the IsSelected-property of all items when the user selects one or more records:

private void DataGrid_SelectedItemsChanged(object sender, Infragistics.Windows.DataPresenter.Events.SelectedItemsChangedEventArgs e)
        {
            (DataContext as FooViewModel).ClearSelectedItems();
            foreach (Record record in DataGrid.SelectedItems.Records)
            {
                ((record as DataRecord).DataItem as FooItem).IsSelected = true;
            }
        }

The datagrid is set to SelectionTypeRecord="Extended", SelectionTypeCell="None" and CellClickAction="SelectRecord".

When clicking(selecting) a record in the grid, also when multiselecting, the record gets selected, the FooItem's IsSelected-property is updated and the CheckBox in the relevant RecordSelector is checked. Seems to work fine!

BUT, when i go to the CheckBox and check/uncheck it, or if the IsSelected-property of the FooItems is updated from code due to the checkbox in HeaderPrefixArea is checked/unchecked, the DataGrid's selection is not updated, or the record's IsSelected is not updated. This results in the datagrid showing records as selected, but the RecordSelector-checkbox isn't checked and the underlying FooItem isn't selected.

Does anyone have a solution or a way I can make the DataGrid's selectedItems update when using the checkbox in the RecordSelector? Is there something i'm doing wrong?

Julian