时间:2017-10-19 09:52:23

标签: c# wpf datagrid

我知道这个问题看起来像之前被问过,但是这个问题有一个转折....

谁可以建议为什么我的数据网格上的这个标题样式除了包装文本之外应该做所有事情。

            // 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
        });

这就是我使用它的方式:

            // 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;

...

如果您知道它为什么不起作用,您是否也可以建议我可以做些什么来使其发挥作用!也许还有其他一些属性可以使用,或者我的某些属性可能是互斥的? (PS - 我并不热衷于XAML解决方案,我在这个网格中有几种不同类型的列,具有非常不同的标题类型和属性) 提前谢谢。

1 个答案:

答案 0 :(得分:0)

首先,TextBlock.TextWrappingProperty不会继承其值。您可以通过以下方式查看:

var pm1 = TextBlock.TextWrappingProperty.GetMetadata(typeof(TextBlock)) as FrameworkPropertyMetadata;
if (pm1 != null)
{
    var test = pm1.Inherits; // false
}

因此,即使该属性设置在DataGridColumnHeader上,也不会在内部TextBlock上设置。因此,如果您想要换行,则需要定位TextBlock的样式资源,或者只需将Header定义为显式文本块:

<DataGridTextColumn.Header>
    <TextBlock Text="aaaaaaaa bbbbbbbbbb cccccccccc ddddddddd" TextWrapping="Wrap"/>
</DataGridTextColumn.Header>

或代码

TotalTextColumn.Header = new TextBlock() { Text = TextBlockEMS_Config_Tool.Properties.Resources.BackupPaths_BackupPaths, TextWrapping = TextWrapping.Wrap };
相关问题