Due to the nature of how a column in the DataGrid is generated it isn't very easy to use a simple style or ones own template column.
But there is a much shorter and better way, we can simply extend the DataGridCheckBoxColumn to provide our own, centered, CheckBox.
public class CenteredCheckBoxColumn
: DataGridCheckBoxColumn
{
protected override FrameworkElement
GenerateEditingElement(
DataGridCell cell, object dataItem)
{
var checkBox =
base.GenerateEditingElement(cell, dataItem);
checkBox.HorizontalAlignment =
HorizontalAlignment.Center;
checkBox.VerticalAlignment =
VerticalAlignment.Center;
return checkBox;
}
protected override FrameworkElement GenerateElement(
DataGridCell cell, object dataItem)
{
var checkBox =
base.GenerateElement(cell, dataItem);
checkBox.HorizontalAlignment =
HorizontalAlignment.Center;
checkBox.VerticalAlignment =
VerticalAlignment.Center;
return checkBox;
}
}
We can now use our CenteredCheckBoxColumn in the same way we use the standard columns.
1 comment:
Very nice approach so far. Couldn't you also publish the used alignments as properties, so we can set them in XAML (probably not bind them, but anyway)?
Post a Comment