OK, I'm having a heck of a time trying to find the magic incantations to make columns automatically size to their content.
Suppose I have three columns: "Size", "Mean" and "Sample Standard Deviation". I would like the Size and Mean columns to squeeze down to some minimum width, say 75 WPF pixels. But I would like the third column to expand to show the full text of the label.
"Autofit"only seems to make all columns the same width, which is not what I want.
As a bonus, I would also like to have each column automatically size to its data, so that if I entered "9999999999" in the size column, then the whole column would automatically grow larger to accommodate the new number.
Thanks,
Denny Huber
In order to do this you also have to specify a template for the grid that will be used to layout the cells in each record, as in the sample xaml below.
Note: The reason we had to take this approach instead of hanging properties off the Field is because in more complex cell layout scenarios, e.g. where cells span mutiple row/columns within each record there is not a one for one correspondence between the Field and the column(s) that contain it within the cell area of a record.
<igDP:FieldLayoutSettings>
<igDP:FieldLayoutSettings.DataRecordCellAreaGridTemplate>
<ItemsPanelTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
</Grid>
</ItemsPanelTemplate>
</igDP:FieldLayoutSettings.DataRecordCellAreaGridTemplate>
</igDP:FieldLayoutSettings>
</igDP:XamDataPresenter.FieldLayoutSettings>
So far so good, but this makes a *very* common scenario rather complex, and if I got that right, it disallows the user to resize the column widths. My current project contains roughly 10 different grids and I'm missing a working resizing behaviour in every one of them, mainly because I want to fill the columns all available space (AutoFit is a joke, I really can't image I'll ever need equally sized columns in a real live application that provides heterogenous data). IMO there is no valid explanation why this shouldn't work like WinGrid - if AutoFit AND cell widths would work together, we had a simple solution.
Just my 0.02$
BTW - I tried to apply a template to a very simple dialog grid, but with mixed results. What I would like to get is a grid that expands the last column to the full size of the grid. Here's the results:
First I tried the star sized last column according to Joe's sample - bad result, text is being cropped, and the other columns are far too wide:
Second try: Setting fixed widths and Auto on the last column. This appears right, but it isn't, as text gets cropped as well - The full string is longer than just "Invariant Language".
Finally, I tried combinding fixed widths with a star-sized column - again, text is being cropped:
Is there a combination I overlooked (also tried not setting a width for the last column at all, same result as the last image)?
Thanks for your advice
Philipp
Hi John
I assume your layout isn't resizable, because you only have two columns, and one of them has a fixed width (100px). This one, for example, works fine:
<ItemsPanelTemplate> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="30" /> <ColumnDefinition Width="150" /> <ColumnDefinition Width="2*" /> <ColumnDefinition Width="4*" /> </Grid.ColumnDefinitions> </Grid></ItemsPanelTemplate>
HTH,Philipp
Phil,
I wonder if I could bother you to send me the ItemsPanelTemplate you are using that has column resizing working. I have done the following and resizing does not work.
<igDP:XamDataGrid x:Name="_dataGrid" DataSource="{Binding StoryList}" AutoFit="True" GroupByAreaLocation="None" >
<igDP:XamDataGrid.FieldLayoutSettings>
<igDP:FieldLayoutSettings AutoGenerateFields="False" SelectionTypeRecord="Single" RecordSelectorLocation="None" >
<ColumnDefinition Width="*" />
</igDP:XamDataGrid.FieldLayoutSettings>
<igDP:FieldLayout>
<igDP:FieldLayout.FieldSettings>
</igDP:FieldLayout.FieldSettings>
<igDP:Field Name="Title" Label="Story Title"/>
</igDP:FieldLayout.Fields>
</igDP:XamDataGrid.FieldLayouts>
The underlying data is simply a class with two properties of type string.
Try setting the Field.Settings.CellHeight, or CellMaxHeight property instead, that won't affect the header area.
My latest posting is still awaiting approval, but here's another comment anyway:
I solved the problem for now by applying a rather ugly hack: I defined a style for the XamTextEditor, which restricts the maximum height for the editor. Like this, the row doesn't resize itself in case of multiline text:
<igDP:XamDataGrid.Resources> <Style TargetType="{x:Type igEditors:XamTextEditor}"> <Setter Property="MaxHeight" Value="22" /> </Style> </igDP:XamDataGrid.Resources>
Joe,
Me again, sorry. I ran into a new issue with the auto-sizing of the grid. I have a field that may provide multi-line text (a lot of). This wasn't a problem until now - the grid just cropped the content, which could be edited through a popup:
However, by assigning a template for the grid, the row's height adjusts to it's content - which is bad if the underlying text consinsts of 50+ lines of text of course. I tried to solve the problem by assigning a RowDefinition for the grid. However, it appears this also affects the header area, which gets badly cropped:
<igDP:FieldLayoutSettings.DataRecordCellAreaGridTemplate> <ItemsPanelTemplate> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="30" /> <ColumnDefinition Width="150" /> <ColumnDefinition Width="2*" /> <ColumnDefinition Width="4*" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="24" /> </Grid.RowDefinitions> </Grid> </ItemsPanelTemplate></igDP:FieldLayoutSettings.DataRecordCellAreaGridTemplate>
Is there a way to get all of this working at the same time?
- auto-sizing- no resizing of the row height (optimum) or fixed row height- independent row height of the header
Edit:
However, a cleaner solution would be welcome.
Cheers,