简体   繁体   English

WPF工具包不正确的DataGrid高度

[英]WPF Toolkit incorrect DataGrid height

I am using the DataGrid control from WPF Toolkit release February 2010 (.net framework 3.5) and after setting text wrap for the cells of the table I have noticed that the DataGrid is displayed with a lot of free space after the last row. 我正在使用WPF Toolkit 2010年2月版(.net framework 3.5)中的DataGrid控件,并且在为表格的单元格设置了文本换行之后,我注意到在最后一行之后,DataGrid的显示空间很大。

The amount of free space seems to be proportional with the numbers of rows (but it's not always the case). 可用空间的数量似乎与行数成正比(但并非总是如此)。

When I manually delete one of the rows, the DataGrid is displayed correctly and the free space is removed. 当我手动删除行之一时,DataGrid将正确显示,并且删除了可用空间。 But if I delete a row in code behind at load time, it has no effect and the free space is still displayed. 但是,如果我在加载时删除了后面的代码行,则该行无效,并且仍然显示可用空间。 My guess is that the trick only works after the DataGrid has been rendered. 我的猜测是,该技巧仅在呈现DataGrid之后才起作用。 Editing the content of a cell does not trigger the resize. 编辑单元格的内容不会触发调整大小。

数据网格

Notes: 笔记:

  1. I tried to set a MaxHeight for the DataGrid but it does not work to well because of the text wrapping. 我试图为DataGrid设置MaxHeight,但由于文本换行而无法正常工作。

  2. I've overwritten the Size MeasureOverride(Size availableSize) method of the DataGrid and noticed that the method is called multiple times, and that at first the size is right and after one or two more calls it increases. 我已经覆盖了DataGrid的Size MeasureOverride(Size availableSize)方法,并注意到该方法被多次调用,起初大小是正确的,再经过一两次调用,它就会增加。 Also availableSize has infinite Height. 同时availableSize具有无限的高度。

Any idea how I can fix the height of the DataGrid? 知道如何固定DataGrid的高度吗?

EDIT: 编辑:

I created a small demo to show case my problem. 我创建了一个小演示来展示我的问题。 The project needs a reference to WPFToolkit. 该项目需要对WPFToolkit的引用。

MainWindow.xaml MainWindow.xaml

MainWindow.xaml.cs MainWindow.xaml.cs

Notice the first and second DataGrid. 注意第一个和第二个DataGrid。

It seems that the DataGridCellsPanel calculates a false height for the DataGrid . 看来, DataGridCellsPanel计算的虚假高度DataGrid

By replacing the DataGridCellsPanel with a StackPanel , the DataGrid calculates the right value for the Height and no more space is wasted. 通过使用StackPanel替换DataGridCellsPanelDataGrid可以为Height计算正确的值,而不会浪费更多的空间。

Solution in XAML: 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>

Solution for code behind, I created a custom control that extends from DataGrid, and has the following code in constructor: 对于背后的代码,我创建了一个自定义控件,该控件从DataGrid扩展,并在构造函数中具有以下代码:

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

Have you tried setting the VerticalContentAlignment and VerticalAlignment to Top? 您是否尝试过将VerticalContentAlignment和VerticalAlignment设置为Top?

I remember having this issue before but I'm not quite sure if that was the required fix. 我记得以前有这个问题,但是我不确定这是否是必需的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM