Reputation: 959
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.
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. 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. Editing the content of a cell does not trigger the resize.
Notes:
I tried to set a MaxHeight for the DataGrid but it does not work to well because of the text wrapping.
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. Also availableSize
has infinite Height.
Any idea how I can fix the height of the DataGrid?
EDIT:
I created a small demo to show case my problem. The project needs a reference to WPFToolkit.
Notice the first and second DataGrid.
Upvotes: 1
Views: 428
Reputation: 959
It seems that the DataGridCellsPanel
calculates a false height for the DataGrid
.
By replacing the DataGridCellsPanel
with a StackPanel
, the DataGrid
calculates the right value for the Height and no more space is wasted.
Solution in 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:
var stackPanelFactory = new FrameworkElementFactory(typeof(StackPanel));
var template = new ItemsPanelTemplate(stackPanelFactory);
this.ItemsPanel = template;
Upvotes: 1
Reputation: 1526
Have you tried setting the VerticalContentAlignment and VerticalAlignment to Top?
I remember having this issue before but I'm not quite sure if that was the required fix.
Upvotes: 0