Reputation: 25
I know this question looks like it has been asked before, but this one has a twist....
Who can suggest why this header styling on my datagrid does everything as it should except wrap the text.
// Setup the Header Style to use on certain of the column headers, centered horizontally and vertically, in bold font, with text wrapping
System.Windows.Style HeaderStyle = new Style();
HeaderStyle.Setters.Add(new System.Windows.Setter
{
Property = FontSizeProperty,
Value = 12.0
});
HeaderStyle.Setters.Add(new System.Windows.Setter
{
Property = System.Windows.Controls.Control.HorizontalAlignmentProperty,
Value = HorizontalAlignment.Stretch
});
HeaderStyle.Setters.Add(new System.Windows.Setter
{
Property = System.Windows.Controls.Control.HorizontalContentAlignmentProperty,
Value = HorizontalAlignment.Center
});
HeaderStyle.Setters.Add(new System.Windows.Setter
{
Property = FontWeightProperty,
Value = FontWeights.Bold
});
HeaderStyle.Setters.Add(new System.Windows.Setter
{
Property = TextBlock.TextWrappingProperty,
Value = TextWrapping.Wrap
});
This is how I use it:
// Add the Backup Paths Total Column
DataGridTemplateColumn TotalTextColumn = new DataGridTemplateColumn();
FrameworkElementFactory TotalTextBorder = new FrameworkElementFactory(typeof(EMSTextCell));
FrameworkElementFactory TotalTextBlock = new FrameworkElementFactory(typeof(TextBlock));
ImageTemplate = new DataTemplate();
TotalTextColumn.CellTemplate = ImageTemplate;
TotalTextColumn.Header = EMS_Config_Tool.Properties.Resources.BackupPaths_BackupPaths;
TotalTextColumn.HeaderStyle = HeaderStyle;
TotalTextColumn.Width = new DataGridLength(100);
TotalTextColumn.CanUserSort = false;
...
If you know why it doesn't work, can you also suggest what I can do to make it work! Maybe there is some other property to use, or perhaps some of my properties are mutually exclusive? (PS - I'm not keen on a XAML solution, I have a few different types of columns in this grid with very different header types and properties) Thanks in advance.
Upvotes: 1
Views: 2021
Reputation: 13458
First of all, the TextBlock.TextWrappingProperty
is not inheriting its value. You can check this with:
var pm1 = TextBlock.TextWrappingProperty.GetMetadata(typeof(TextBlock)) as FrameworkPropertyMetadata;
if (pm1 != null)
{
var test = pm1.Inherits; // false
}
So, even though the property is set on DataGridColumnHeader
, it is not set on the inner TextBlock
. So if you want wrapping, you need a style resource targeting TextBlock
or just define the Header
as an explicit textblock:
<DataGridTextColumn.Header>
<TextBlock Text="aaaaaaaa bbbbbbbbbb cccccccccc ddddddddd" TextWrapping="Wrap"/>
</DataGridTextColumn.Header>
or in code
TotalTextColumn.Header = new TextBlock() { Text = TextBlockEMS_Config_Tool.Properties.Resources.BackupPaths_BackupPaths, TextWrapping = TextWrapping.Wrap };
Upvotes: 1