Silverlight Datagrid 行高不会降低

Silverlight Datagrid row height won't decrease

我正在将 ObservableCollection 绑定到 Datagrid。除了这个行高问题之外,一切正常,我已经为此奋斗了一段时间。

问题是行高存储的是最大单元格的高度,并且不会从中改变。

如果我有 5 个对象的集合。在 ASC 顺序中,第 1 行高度为 100,第 5 行高度为 20。如果我将同一列求助于 DESC,则第 1 行高度现在为 100,第 5 行高度也为 100。

我尝试将 Datagrid 包装在 Scrollviewer 中,并将 DataGridTextColumn 更改为具有垂直对齐文本框的 DataGridTemplateColumn。 Silverlight 没有 ViewCollectionSource,所以我无法尝试那个。

如何让它在排序后重新计算高度?

XAML

        SelectedItem="{Binding SelectedComment, Mode=TwoWay}"
        VerticalAlignment="Top"
        Width="1000"
        Height="Auto"
        >
         <sdk:DataGrid.Columns>
                <sdk:DataGridTemplateColumn Header ="Comment" Width="4*" IsReadOnly="True" SortMemberPath="Commentstr" CanUserSort="True">
                    <sdk:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Commentstr}" TextWrapping="Wrap" VerticalAlignment="Top"></TextBlock>
                        </DataTemplate>
                    </sdk:DataGridTemplateColumn.CellTemplate>
                </sdk:DataGridTemplateColumn>

            </sdk:DataGrid.Columns>
        </sdk:DataGrid>

代码隐藏

 private ObservableCollection<Comment> commentCollection = new ObservableCollection<Comment>();
    public ObservableCollection<Comment> CommentCollection
    {
        get { return commentCollection; }
        set
        {
            commentCollection = value;
        }
    }
    public Main()
    {
        InitializeComponent();
        this.Loaded += new RoutedEventHandler(CustomScreenTemplate_Loaded);

        CommentGrid.ItemsSource = CommentCollection;
    }

终于解决了行高问题。在这里发布对我有用的内容,以防其他人遇到同样的问题。

为 DataGrid

创建覆盖 Class
public class FixedDataGrid : DataGrid
{
    /// <summary>
    /// Overrides OnLoadingRow
    /// </summary>
    protected override void OnLoadingRow(DataGridRowEventArgs e)
    {
        if (double.IsNaN(this.RowHeight))
        {
            e.Row.Loaded += Row_Loaded;
        }

        base.OnLoadingRow(e);
    }

    private void Row_Loaded(object sender, RoutedEventArgs e)
    {
        var row = (DataGridRow)sender;
        row.Loaded -= Row_Loaded;

        for (int col = 0; col < this.Columns.Count; col++)
        {
            var cellElement = this.Columns[col].GetCellContent(row);
            if (cellElement != null)
            {
                cellElement.SizeChanged += CellElement_SizeChanged;
            }
        }
    }

    private static void CellElement_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        //Fix issue: DataGrid Row Auto-Resize only grows row height but won't shrink
        var dataGrid = GetParentOf<DataGrid>((FrameworkElement)sender);
        if (dataGrid != null && double.IsNaN(dataGrid.RowHeight))
        {
            var row = DataGridRow.GetRowContainingElement((FrameworkElement)sender);

            //Fore recalculating row height
            try
            {
                dataGrid.RowHeight = 0;
                row.InvalidateMeasure();
                row.Measure(row.RenderSize);
            }
            finally
            {
                //Restore RowHeight
                dataGrid.RowHeight = double.NaN;
            }
        }
    }

    private static T GetParentOf<T>(FrameworkElement element) where T : FrameworkElement
    {
        while (element != null)
        {
            T item = element as T;
            if (item != null)
            {
                return item;
            }

            element = (FrameworkElement)VisualTreeHelper.GetParent(element);
        }

        return null;
    }
}

使用覆盖 class 作为您的数据网格

xmlns:fixed="clr-namespace:MyNameSpace

<fixed:FixedDataGrid x:Name="BetterGrid"  ... />