Hello everybody,
I have the feeling that this is a very basic and stupid question, but I am having hard time anyway... so
I have a window hosting a XamGrid. I am setting the columns. This is the very simple declaration
<ig:XamGrid x:Name="dataGrid" AutoGenerateColumns="False" ItemsSource="{Binding Path=Properties_w}" ColumnWidth="Auto"><ig:XamGrid.ColumnResizingSettings><ig:ColumnResizingSettings AllowCellAreaResizing="True" AllowColumnResizing="Disabled"/></ig:XamGrid.ColumnResizingSettings><ig:XamGrid.Columns><ig:TextColumn Key="PropertyName" HeaderText="{locExt:Localizer prop}"/><ig:TextColumn Key="Category" HeaderText="{locExt:Localizer category}"/><ig:TextColumn Key="SubscribedTo" /><ig:TextColumn Key="DataType" /><ig:TextColumn Key="Units" /><ig:TextColumn Key="UserAccess" /><ig:TextColumn Key="ConstraintStatus" Width="*"/></ig:XamGrid.Columns></ig:XamGrid>
As you can see, I set Auto for every column's width, except for the last one (*) which will take all available space.
I simply want my window to adapt to the width of the grid.
How can i do that? If I set Width="Auto" the window will (don't know why) grow more than necessary. If I set a constant number (e.g. Width="500") the window won;t resize (scrollbars will appear). If I set SizeToContent="Width" the window will grow, grow grow...
Why is that so hard:? What am I missing?
The window will simply host this grid and I want it to have the proper size
PS: I am using XamGrid, but if you have a suggestion for XamDataGrid I can use it, no problem
Thanks in advance!
Hello,
I raised a similar question about this several years ago, I can't find the link now. But basically the xamGrid is designed to behave like the following:
1) The xamGrid will stretch to fill the entire available space provided to it
2) If the available space is Infinity then it will size to fit (fully render its content)
So since your xamGrid is placed in a window with no max sizes specified, the available size of the window will be the size of your screen/monitor (not infinity). This is the available size that is passed to the xamGrid for rendering and the xamGrid will stretch to fill this available space.
If you instead place your xamGrid inside of a scrollviewer within the window, then the available space passed into the xamGrid will be Infinity. In this case the xamGrid will render all of it's content and size itself accordingly. This might fix your problem if you don't have very many columns, however when using a scrollviewer you force the xamGrid to fully render it's contents and thus loses all virtualization functionality and could have a large performance impact.
I don't like the way it works, but this is how the xamGrid is designed to behave, as far as I can tell through different tests.
-Chris
Hello Valentina,
In order to update the size (Width and Height) of the Window with the size of the XamGrid (whenever it is changed), you can simply handle the SizeChanged event of the XamGrid and use the e.Size.Width and the e.Size.Height properties for updating the Window's size.
private void grid_SizeChanged(object sender, SizeChangedEventArgs e){ this.Width = e.NewSize.Width; this.Height = e.NewSize.Height;}
I have modified the sample from my previous reply, by using the approach from above.
If you have any further questions, please let me know
Thanks Tacho for your feedback.
However, I am afraid I was not clear in my question
What I am trying to achieve is: is there a way to make the Window resize according to Grid content?
I can see from your xaml that you have set for the window: Height="600" Width="800". This way, the window will have 800 as width, and the grid will resize accordingly (with *, all columns will have same width).
I mean, all I want is exactly the other way around: I want all columns to have “Auto” width (according to their header/cell size), and the window must resize accordingly. Is there a way to do that?
PS: I couldn't find any images in the zip
Thanks again
Valentina
Thank you for the code-snippet you have provided.
The behavior you have described in regards to all columns being with an auto width except for the last one (which takes up all the remaining space) seems to be working as expected. (refer to 2 in the grids.png image inside the zip) In this case, when resizing the window, the width of the last column gets smaller as well.
In order for all columns to have the same width at all time by taking up all of the window's space and dividing it equally, you can set the ColumnWidth property of the XamGrid to * (analogically in the XamDataGrid, you can set the AutoFit property to True). (refer to 3 in the grids.png image inside the zip)
I have attached a sample application that uses the approaches from above.
If you have any questions, please let me know.