I'm creating a DataGrid
in C# (from code-behind/not XAML), but no matter what I try, I can't get the text to be vertically center in the data cells:
I started off with:
var CellStyle = new Style(typeof(DataGridCell)) {
Setters = {
new Setter(TextBlock.TextAlignmentProperty, TextAlignment.Center)
}
};
Which correctly targets the cell and horizontally centres the text (per the screenshot above).
Trying to vertically center the text, I know that a TextBlock
doesn't support vertical content alignment, only its own vertical alignment within a parent element.
Per this question (Text vertical alignment in WPF TextBlock) I tried to fake it using Padding
:
var CellStyle = new Style(typeof(DataGridCell)) {
Setters = {
new Setter(TextBlock.PaddingProperty, new Thickness(5)),
new Setter(TextBlock.TextAlignmentProperty, TextAlignment.Center)
}
};
This made no difference. Then I tried this:
var CellStyle = new Style(typeof(DataGridCell)) {
Setters = {
new Setter(DataGridCell.VerticalContentAlignmentProperty, VerticalAlignment.Center),
new Setter(TextBlock.TextAlignmentProperty, TextAlignment.Center),
new Setter(TextBlock.VerticalAlignmentProperty, VerticalAlignment.Center)
}
};
Which resulted in:
Adding new Setter(DataGridCell.HeightProperty, 50d),
results in screenshot #1.
How can I vertically center the text in my data cells?
My answer just summarizes King King's, in case it helps someone. In XAML:
Use the property
CellStyle="{StaticResource CustomCell}"
in yourDataGrid
whereUsing Blend for Visual Studio, we have this style for the
DataGridCell
:So looks like there is not any default support for changing the alignments here. Normally the
<ContentPresenter>
should have code like this:Then we can change the
VerticalContentAlignment
andHorizontalContentAlignment
in the style of theDataGridCell
to change the alignments.That means if using XAML code, your solution can be solved by just appending the above code. But if you want to use code behind, it's of course much longer and more complicated.
Here I introduce to you 2 solutions. First by building the VisualTree for the
ControlTemplate
and set that template for theTemplate
property ofDataGridCell
:The second solution is by using
XamlReader
to parse the XAML code directly, that means we need the exact XAML code as given before saved in a string andXamlReader
will parse that string giving out an instance ofStyle
:You can see that both solutions are fairly long but they are actually what you should do using code behind. That means we should always use XAML code as much as possible. Many features in WPF are designed mainly for XAML code, so using code behind is of course not straightforward and usually verbose.
NOTE: The
XAML
code I posted at the beginning is not the full default style forDataGridCell
, it has some moreTriggers
. That means the code may be much longer, sorry, here is the full default XAML code:However I've just tested it, looks like the default style is always applied to the
DataGridCell
, it's just overridden by theSetter
you added (which set the same properties). Here is the testing code, theTrigger
still works: