【问题标题】:Silverlight Datagrid row height won't decreaseSilverlight Datagrid 行高不会减少
【发布时间】:2017-03-11 13:51:17
【问题描述】:

我将 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;
    }

【问题讨论】:

    标签: silverlight datagrid


    【解决方案1】:

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

    为 DataGrid 创建一个重写类

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

    使用覆盖类作为你的数据网格

    xmlns:fixed="clr-namespace:MyNameSpace
    
    <fixed:FixedDataGrid x:Name="BetterGrid"  ... />
    

    【讨论】:

      猜你喜欢
      • 2021-10-01
      • 1970-01-01
      • 2011-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多