Jai,
thank you for the sample. Getting the checkbox in the header was easier than I thought.
The behaviour was not exactly what I wanted to achieve. I wanted to sync the Checkboxes with the selection an vice versa, so if you click in the checkbox column you add/remove one entry from the selection and when you click any other column the checkboxes are automatically turned on/off corresponding to the selection.
I managed to change the code to almost behave as I want it to. See here:
protected override void ProcessSelectedItemsChanged(NotifyCollectionChangedEventArgs e)
{
base.ProcessSelectedItemsChanged(e);
if (e.Action == NotifyCollectionChangedAction.Reset)
{
foreach (OrderInfo o in (this.DataGrid.DataContext as ViewModel).OrderInfoCollection)
o.IsChecked = false;
}
else if (e.Action == NotifyCollectionChangedAction.Add)
{
foreach (OrderInfo o in e.NewItems)
o.IsChecked = true;
}
else if (e.Action == NotifyCollectionChangedAction.Remove)
{
foreach (OrderInfo o in e.OldItems)
o.IsChecked = false;
}
else
{
if (e.NewItems != null)
foreach (OrderInfo o in e.NewItems)
o.IsChecked = true;
if (e.OldItems != null)
foreach (OrderInfo o in e.OldItems)
o.IsChecked = false;
}
}
protected override void ProcessPointerReleased(MouseButtonEventArgs args, RowColumnIndex rowColumnIndex)
{
if (rowColumnIndex.ColumnIndex == 0)
{
List<object> Selection = new List<object>();
foreach (GridRowInfo gri in this.SelectedRows)
Selection.Add(gri);
base.ProcessPointerReleased(args, rowColumnIndex);
// Was the current row in the selection before?
object griCurrent = Selection.SingleOrDefault(item => ((GridRowInfo)item).RowIndex == rowColumnIndex.RowIndex);
if (griCurrent != null)
{
Selection.Remove(griCurrent);
}
else
{
griCurrent = this.SelectedRows.SingleOrDefault(item => item.RowIndex == rowColumnIndex.RowIndex);
if (griCurrent != null)
Selection.Add(griCurrent);
}
this.ClearSelections(false);
if (Selection.Count > 0)
this.AddSelection(Selection, SelectionReason.SelectedItemsChanged);
}
else
{
base.ProcessPointerReleased(args, rowColumnIndex);
var SelectedRows = this.SelectedRows.FindAll(item => item.RowIndex == rowColumnIndex.RowIndex);
if (SelectedRows.Count == 0)
{
var row = this.DataGrid.GetRecordAtRowIndex(rowColumnIndex.RowIndex);
(row as OrderInfo).IsChecked = false;
}
else
(SelectedRows[0].RowData as OrderInfo).IsChecked = true;
}
var collectioncount = (DataGrid.DataContext as ViewModel).OrderInfoCollection.Count;
var selecteditemcount = DataGrid.SelectedItems.Count;
if (selecteditemcount == collectioncount)
(this.DataGrid.DataContext as ViewModel).IsHeaderChecked = true;
else if (selecteditemcount == 0)
(this.DataGrid.DataContext as ViewModel).IsHeaderChecked = false;
else
(this.DataGrid.DataContext as ViewModel).IsHeaderChecked = null;
args.Handled = true;
DataGrid.Focus();
}
There Is no just one thing I am missing but it looks like it is a limitation of your Grid control.
I use Extended selection mode and select a couple of items by dragging the mouse and holding the Ctrl key. If I now try to select an second buch of items while still holding the Ctrl key the previous selection is clear.
Is this a limitation or can I realize multiple block selection?
Regards
Bernd