I have an SfDataGrid bound, via ItemsSource, to an instance of a class derived from ObservableCollection<>. The instance is a static resource defined in App.xaml because the contents are shared among several controls and several IHostedServices.
One of the services loads the collection with information about image files in a user-selected directory. That information includes the full path to the image file and the date the file was created.
Unfortunately, while the SfDataGrid updates properly (to show all the captured information) the width of the various columns does not automatically update. Instead, the various display strings are wrapped.
I don't understand why this is happening because the XAML for the SfDataGrid implies it should automatically update:
<dataGrid:SfDataGrid Height="300"
AutoGenerateColumns="False"
ColumnWidthMode="Auto"
AllowResizingColumns="True"
ItemsSource="{StaticResource SharedFileInfo}">
<dataGrid:SfDataGrid.Columns>
<dataGrid:GridTextColumn MappingName="DisplayPath"
HeaderText="File Path"/>
<dataGrid:GridDateColumn MappingName="DateCreated"
HeaderText="Date Created"
DisplayDateFormat="MM/dd/yyyy HH:mm:ss"/>
<dataGrid:GridDateColumn MappingName="DateTaken"
HeaderText="Date Taken"
DisplayDateFormat="MM/dd/yyyy HH:mm:ss"/>
</dataGrid:SfDataGrid.Columns>
</dataGrid:SfDataGrid>
The same behavior occurs if I set ColumnWidthMode to SizeToCells.
How do I get the columns to size properly? I tried raising a "collection reset" event on the source collection, but that didn't have any effect.
Separately, there is an odd behavior that occurs if you hover the mouse over the right-hand edge of the last column's header and try to resize the last column by clicking and dragging: generally no width change takes place...and sometimes the last column vanishes. I presume this means its width got set to 0. But whatever the cause it's really annoying. How do I give the user the ability to resize the last
Understood. Here is a link to the current software: markolbert/MediaTimestampUpdater at dev-change-sharing (github.com)
Let me know if you need more information.
Sorry, I don't have time to put together a simple example (besides, the project I sent you isn't all that complicated).
I'm not sure why you couldn't find the other assemblies (e.g., ColorfulHelp). They're all available on nuget.org. You may have to "convert" them from project references to
|
Unfortunately, while the SfDataGrid updates properly (to show all the captured information) the width of the various columns does not automatically update. Instead, the various display strings are wrapped. |
We are able to understand this scenario. We must refresh the autosize calculation at runtime by calling SfDataGrid.ColumnSizer.Refresh method. Please refer the below given help documentation.
|
|
Separately, there is an odd behavior that occurs if you hover the mouse over the right-hand edge of the last column's header and try to resize the last column by clicking and dragging: generally no width change takes place...and sometimes the last column vanishes. I presume this means its width got set to 0. But whatever the cause it's really annoying. How do I give the user the ability to resize the last |
We are unable to reproduce any issue while resizing the last column. The sample we have used to check this case is available in the following link for your reference.
Please have a look at this sample and let us know if you have missed any customization you have made in your application. Otherwise please try to reproduce the issue in this sample and revert to us with the modified sample and video illustration of the issue. It will be more helpful for us to find the exact cause for the issue and to provide a prompt solution. |
Thanx, Mohanram. But, unfortunately, calling ColumnSizer.ResetAutoCalculationforAllColumns() doesn't work. The column size is adjusted...but not properly.
A little more background: the SfDataGrid control is on a Page attached to a viewmodel. The viewmodel updates the ObservableCollection to which the SfDataGrid is bound. So as not to couple the viewmodel to the view, the viewmodel cannot directly call the ColumnSizer resizing method. Instead, it sends a message (via WeakReferenceMessenger; I'm using the Windows Community Toolkit) which the Page control registers for and processes.
I mention all this because I'm not sure if that structure is interfering with how the resizing method works. I don't think it should...but it's your control, not mine :).
One other potentially important piece of evidence: I did a test case where I converted the column to a plain old GridTextColumn bound to a long string. I can see the ColumnSizer changing the column width...but it "insists" on wrapping the text on the last word (i.e., the last word appears on a second line within the cell). It's almost as if whatever UI element is displaying the text has auto wrap turned on.
If you have additional suggestions for me to try and get the column sizing to work I'd appreciate hearing them
Hi Mohanram, I'm replying to the second issue I raised separately here (i.e., the one about problems with trying to use click-and-drag to change the width of the last column in the SfDataGrid).
Some more information: the last column can be resized...but it's really tricky to do, for two reasons.
First, the automatically-generated vertical scroller extends all the way up into the header row. This seems unnecessary to me...and the scroll bar's thumb control seems to "hide" the grid's ability to detect that a mouse is hovering over the right hand edge of the last column preparatory to doing the click-and-drag to resize it.
To get around this first issue what I generally to is scroll the grid down a few rows so the scroller thumb isn't up in the header row. This "unhides" the right hand edge of the last column for dragging.
But even then grabbing the edge is really finicky: it feels like being "off" the edge by more than a couple of pixels prevents you from grabbing it. You can see this by watching the mouse cursor: it rapidly snaps back and forth between being a normal cursor and being the "click me to drag the width" cursor.
Eventually I end up being able to hover in just the correct place, click and hold the left mouse button to initiate the column drag, and am able to adjust the column width. But it's a pain in the butt.
Interestingly, this sensitivity doesn't appear on any of the other column edges. It's just on the last column. Perhaps there's some interaction between the cell boundary and the vertical scroller?
I hope this additional information helps explain the second issue.