如何以编程方式设置WPF DataGrid TextTrimming属性

时间:2015-10-09 20:45:46

标签: c# wpf datagrid

我有一些WPF DataGrid在C#代码中非常漂亮,如下所示。 TextAlignmentTextWrappingToolTip都正常运行。但是,我无法显示省略号TextTrimming。你能告诉我我在这里做错了吗?

public static void FormatGrid(ref DataGrid oDataGrid, List<ColumnFormat> oColumnFormats)
{
    oDataGrid.CanUserResizeColumns = true;
    SetHighlightBrush(ref oDataGrid, oUser);
    oDataGrid.Background = GetBackgroundBrush(oUser);
    oDataGrid.Foreground = GetBrushForeground(oUser, ColorType.Medium);

    for (int iColumn = 0; iColumn < oDataGrid.Columns.Count; iColumn++)
    {
        DataGridColumn oColumn = oDataGrid.Columns[iColumn];
        ColumnFormat oColumnFormat = GetColumnFormat(oColumn);
        if (oColumn.Visibility == Visibility.Visible)
        {
            if (oColumnFormat != null)
            {
              oColumn.CellStyle = GetCellStyle(oColumnFormat.ColumnName);
            }
        }
    }
}

private static Style GetCellStyle(string sToolTipPath)
{
    Style oCellStyle = new Style(typeof(DataGridCell));
    SetterBaseCollection oSetters = GetStyleSetters(sToolTipPath);
    foreach (Setter oSetter in oSetters)
    {
        oCellStyle.Setters.Add(oSetter);
    }
    return oCellStyle;
}

private static SetterBaseCollection GetStyleSetters(string sToolTipPath)
{
    SetterBaseCollection oSetters = new SetterBaseCollection();
    Setter oTextAlignment = new Setter(TextBlock.TextAlignmentProperty, TextAlignment.Left);
    oSetters.Add(oTextAlignment);
    Setter oTextWrapping = new Setter(TextBlock.TextWrappingProperty, TextWrapping.NoWrap);
    oSetters.Add(oTextWrapping);
    Setter oTextTrimming = new Setter(TextBlock.TextTrimmingProperty, TextTrimming.CharacterEllipsis);
    oSetters.Add(oTextTrimming);
    Setter oToolTip = new Setter(TextBlock.ToolTipProperty, new Binding(sToolTipPath));
    oSetters.Add(oToolTip);
    return oSetters;
}

1 个答案:

答案 0 :(得分:0)

我无法确切地告诉你问题的原因是什么,除了看起来TextTrimming是一个特殊的情况,无论XYZ的原因。即使尝试通过XAML设置它,这也适用于我的自定义DataGrid CellStyle:

<Setter Property="TextBlock.Foreground" Value="Red"/>

虽然这不是:

<Setter Property="TextBlock.TextTrimming" Value="CharacterEllipsis"/>

长话短说,我有一个类似的问题,可能是你通过CodeBehind进行调整的解决方案。它需要覆盖DataGrid的AutoGeneratingColumn事件,然后使用自定义列在生成元素时设置TextTrimming属性。奇迹般有效。这里有一些快速的伪代码可以帮助你入门(在VB中,我的道歉!):

    Private Sub CustomDataGrid_AutoGeneratingColumn(sender As Object, e As DataGridAutoGeneratingColumnEventArgs) Handles Me.AutoGeneratingColumn
        e.Cancel = True
        Dim binding As New Binding
        Dim textColumn = TryCast(e.Column, DataGridTextColumn)
        If textColumn IsNot Nothing Then
            binding = textColumn.Binding
            binding.TargetNullValue = "-"
        End If
        Columns.Add(New CustomColumn() With {.Header = e.Column.Header, .Binding = binding})
    End Sub

...

Public Class CustomColumn
Protected Overrides Function GenerateElement(cell As DataGridCell, dataItem As Object) As FrameworkElement
    Dim block As New TextBlock()
    Dim column As CustomColumn = DirectCast(cell.Column, CustomColumn)
    Dim binding As Binding = DirectCast(column.Binding, Binding)
    If binding IsNot Nothing Then
        Dim cellBinding As New Binding(binding.Path.Path)
        cellBinding.Source = dataItem
        cellBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
        cellBinding.ValidatesOnDataErrors = True
        cellBinding.ValidatesOnExceptions = True
        cellBinding.NotifyOnValidationError = True
        cellBinding.ValidatesOnNotifyDataErrors = True
        cellBinding.Mode = BindingMode.OneWay
        block.SetBinding(TextBlock.TextProperty, cellBinding)
        block.TextTrimming = TextTrimming.CharacterEllipsis
    End If

    Return block
End Function
End Class