I would like to modify the selection behavior of the DataGrid in the following way. Normally when you have multiple rows selected, and then you click one of the items already se
Note: This answer only tries to provide a solution to following issue mentioned in the question; not how to override the grid's selection behavior. I am hoping that once you have a custom DataGridCell
in place, it can be a good starting point for what you are trying to do.
However, I'm having trouble getting the DataGrid to create a MultiDragDataGridCell instead of a normal DataGridCell, since the class that instantiates DataGridCell is internal. Anyone know how I can achieve that..
Solution: In order to ensure that the DataGrid
uses your custom DataGridCell
- you need to re-template your DataGridRow
to use an extended version of DataGridCellsPresenter
which in-turn will provide your custom DataGridCell
.
Please refer following sample code:
Extending DataGrid controls
public class ExtendedDataGrid : DataGrid
{
protected override DependencyObject GetContainerForItemOverride()
{
//This provides the DataGrid with a customized version for DataGridRow
return new ExtendedDataGridRow();
}
}
public class ExtendedDataGridRow : DataGridRow { }
public class ExtendedDataGridCellsPresenter : System.Windows.Controls.Primitives.DataGridCellsPresenter
{
protected override DependencyObject GetContainerForItemOverride()
{
//This provides the DataGrid with a customized version for DataGridCell
return new ExtendedDataGridCell();
}
}
public class ExtendedDataGridCell : DataGridCell
{
// Your custom/overridden implementation can be added here
}
Re-template DataGridRow in XAML (a more comprehensive template can be found at this link - I am only using a watered-down version of it for sake of readability).
And, your extended DataGrid
's visual tree have the custom datagrid-cells:
Also, please note, it is not mandatory to extend DataGrid
, or DataGridRow
to provide a custom DataGridCell
- you can achieve the same result by just extending DataGridCellsPresenter
(and, updating DataGridRow
's control-template to use the extended version)