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
NM there is a selectedItems member. Carry on!
Ouch we have to have an object maintain information with respect to the selected records of a datapresenter? Was looking for selectedItems or something similar. I'll keep looking and hope that this isn't the case.
Yes, I have the same issue as the initial thread. I need DataGrid's selectedItems to get updated when using the checkbox in the RecordSelector is checked/unchecked?
Hello,
You can see this implemented in the initial post of this thread.
Hi Alex,
I am facing a similar issue. I need to have the xamgrid row IsSelected = true/falsebased on the value of the checkbox (checked/unchecked). Can you please tell me what style and trigger should be in place to achieve this based on DataItem.IsChecked value?
Thanks!!