【问题标题】:WPF Toolkit incorrect DataGrid heightWPF 工具包不正确的 DataGrid 高度
【发布时间】:2013-03-17 14:01:13
【问题描述】:

我正在使用来自WPF Toolkit release February 2010 (.net framework 3.5) 的 DataGrid 控件,在为表格的单元格设置文本换行之后,我注意到 DataGrid 在最后一行之后显示有很多可用空间。

可用空间的数量似乎与行数成正比(但并非总是如此)。

当我手动删除其中一行时,DataGrid 会正确显示并删除可用空间。但是如果我在加载时删除代码后面的一行,它没有任何效果,并且仍然显示可用空间。我的猜测是这个技巧只有在 DataGrid 被渲染后才有效。编辑单元格的内容不会触发调整大小。

注意事项:

  1. 我尝试为 DataGrid 设置 MaxHeight,但由于文本换行,它无法正常工作。

  2. 我已经覆盖了 DataGrid 的 Size MeasureOverride(Size availableSize) 方法,并注意到该方法被多次调用,并且一开始大小是正确的,然后再调用一两次后它会增加。 availableSize 也有无限的高度。

知道如何修复 DataGrid 的高度吗?

编辑:

我创建了一个小演示来展示我的问题。该项目需要对 WPFToolkit 的引用。

MainWindow.xaml

MainWindow.xaml.cs

注意第一个和第二个 DataGrid。

【问题讨论】:

    标签: c# wpf datagrid wpftoolkit


    【解决方案1】:

    似乎DataGridCellsPanel 为DataGrid 计算了错误的高度。

    通过将DataGridCellsPanel 替换为StackPanel,DataGrid 可以计算出正确的高度值,并且不会浪费更多空间。

    XAML 中的解决方案:

    <wpf:DataGrid ItemsSource="{Binding DataGridDS, ElementName=mainWindow}"
                  VirtualizingStackPanel.IsVirtualizing="False">
        <wpf:DataGrid.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel />
            </ItemsPanelTemplate>
        </wpf:DataGrid.ItemsPanel>
        <wpf:DataGrid.Columns>
            <wpf:DataGridTextColumn Binding="{Binding Col1}" Width="*" />
            <wpf:DataGridTextColumn Binding="{Binding Col2}" Width="100" />
            <wpf:DataGridTextColumn Binding="{Binding Col3}" Width="100" />
        </wpf:DataGrid.Columns>
    </wpf:DataGrid>
    

    代码后面的解决方案,我创建了一个从DataGrid扩展的自定义控件,并且在构造函数中有以下代码:

            var stackPanelFactory = new FrameworkElementFactory(typeof(StackPanel));
            var template = new ItemsPanelTemplate(stackPanelFactory);
            this.ItemsPanel = template;
    

    【讨论】:

      【解决方案2】:

      您是否尝试将 VerticalContentAlignment 和 VerticalAlignment 设置为 Top?

      我记得以前遇到过这个问题,但我不太确定这是否是必需的解决方法。

      【讨论】:

      • 是的,我试过了,但没有用。我实际上将 VerticalContentAlignment 和 VerticalAlignment 设置为 Top 到层次结构中的所有项目。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-08
      • 2010-12-19
      • 1970-01-01
      • 2018-10-06
      • 1970-01-01
      • 1970-01-01
      • 2010-12-17
      相关资源
      最近更新 更多