Home Dashboard Directory Help
Search

Silverlight 4 DataGrid Star Column Width by Tony Lugg


Status: 

Active


5
0
Sign in
to vote
Type: Bug
ID: 559644
Opened: 5/17/2010 11:32:45 AM
Access Restriction: Public
3
Workaround(s)
view
4
User(s) can reproduce this bug

Description

I have an issue that is driving me crazy. My page (navigation content frame) is displayed inside a scrollviewer so that scroll bars will appear if the browser is sized smaller than my page. I have a grid that resizes its second column and fills any available space in the browser, and its minimum width is set to a specific value for the case where the browser window is reduced. Within the second grid column I have a DataGrid.

This was working very well until I tried to use the new DataGrid star column width. I want the third data column to fill the "remaining" space within the DataGrid. As soon as I set that DataGrid column's width to Star my page width expands far to the right. I can't seem to determine how to prevent this from happening.

In short, I want the column to resize to the grid, but I don't want the grid to resize.

The Grid behaves expectedly as per this example:

<UserControl>
<ScrollViewer>
<Grid>
<Grid.ColumnDefinitions>
    <ColumnDefinition Width="Auto"/>
    <ColumnDefinition Width="*" MinWidth="325"/>
</Grid.ColumnDefinitions>
</Grid>
</ScrollViewer>
</UserControl>


The above code causes the Grid column 1 to size to the remaining width of its container even though there is no explicit width on the Grid.

In my opinion, the DataGrid should allow the same behaviour, if not be default, then at least as an option.
Details
Sign in to post a comment.
Posted by teaspoon on 4/24/2012 at 2:57 AM
Same unexpected behavior still present in SL5.
Posted by th4z on 1/24/2012 at 5:57 AM
It's a shame that this has not been fixed yet. Anyone tried it with SL5?
Posted by Kir Birger on 4/13/2011 at 9:10 AM
It's been almost a year. I would expect that when issues are as clear cut and simple to reproduce as this that we would get some sort of response from the support team.

As a note, the "grid" in the above example is actually not needed. You DO actually need to populate the DataGrid, I think. I would expect that when verifying/testing/trying to reproduce this, the person tasked with handling this issue would actually try that.
Posted by Chris Bordeman on 9/27/2010 at 7:15 PM
Really need a solution on this. Also, a similar thing happens on lists and datagrids vertically when data rows exist. When contained in a ScrollViewer, for instance a datagrid that is filled with data will grow large enough to display all rows, disregarding the client size of the container (the ScrollViewer).
Posted by Tony Lugg on 6/4/2010 at 5:20 PM
Can I assume you (Microsoft) are no longer interested in this bug?
Posted by Tony Lugg on 5/26/2010 at 10:19 AM
Hello, did you recieve the demo I attached?
Posted by Tony Lugg on 5/21/2010 at 5:53 AM
Hello, on May 18th I created a demo project to demonstrate the issue as requested and attached it. Did you not receive it?
Posted by Microsoft on 5/21/2010 at 12:42 AM
I am currently standing by for an update from you and would like to know how things are going on your end. If you could get back to me at your earliest convenience with information I request, we will be able to make headway towards a resolution. I look forward to hearing from you.
Posted by Microsoft on 5/18/2010 at 12:14 AM
Thanks for reporting this issue. In order to fix the issue, we must first reproduce the issue in our labs. We are unable to reproduce the issue with the steps you provided.

Please give us a demo project to demonstrate this issue so that we can conduct further research.

It would be greatly appreciated if you could provide us with that information as quickly as possible. If we do not hear back from you within 7 days, we will close this issue.

Thanks again for your efforts and we look forward to hearing from you.

Visual Studio Product Team
Posted by Microsoft on 5/17/2010 at 5:05 PM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)
Sign in to post a workaround.
Posted by th4z on 1/24/2012 at 6:09 AM
There is another workaround I found somewhere on Stackoverflow (credits go to whoever posted this!). It is only suitable if you want to achieve that the columns take up the whole space of the DataGrid AND all of them should be sized equally.

You have to hook up an event handler to the SizeChanged event:

/// <summary>Resizes all columns automatically based on the available space inside the datagrid.
/// Workaround for the buggy behavior of columns with "*" width.</summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The event.</param>
private void DataGridSizeChanged(object sender, SizeChangedEventArgs e)
{
    DataGrid dataGrid = (DataGrid)sender;

    // Do not change column size if Visibility State Changed
    if (dataGrid.RenderSize.Width != 0)
    {
        double allColumnsSizes = dataGrid.Columns.Sum(dgC => dgC.ActualWidth);

        // Space available to fill ( -18 Standard vScrollbar)
        double spaceAvailable = (dataGrid.RenderSize.Width - 18) - allColumnsSizes;
        foreach (DataGridColumn dgC in dataGrid.Columns)
        {
            dgC.Width = new DataGridLength(dgC.ActualWidth + (spaceAvailable / dataGrid.Columns.Count));
        }
    }
}

The size of the columns themselves must be set to "Auto" in XAML.

This solution may not be the best because you might experience some layout runtime errors and I have not found a way yet to use GroupDescriptors in combination with RowDetails - everytime the RowDetails expand the app crashes, but this could possibly be related to another bug (it works perfectly without the automatic resizing, however).
Posted by Chris Bordeman on 9/27/2010 at 12:58 PM
That workaround doesn't work unless the container is the top level item in the app.
Posted by Nathan Chantry on 9/22/2010 at 5:42 AM
If anyone is still interested, I encountered exactly the same problem. The workaround I used was having the control that hosted the scrollviewer detecting its own SizeChanged event and setting the width of the scrollviewer's Content object to Application.Current.Host.Content.ActualWidth. Doing this effectively forces the ExtentWidth of the ScrollViewer - ensuring that it doesn't actually scroll horizontally, just vertically. See snippet below. Hope it helps. Been burning the midnight oil trying to solve this one...

<ScrollViewer x:Name="RootScroller" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden">
            <Grid...>
</ScrollViewer>

    Private Sub OnSizeChanged(ByVal sender As Object, ByVal e As EventArgs)
        If TypeOf Me.RootScroller.Content Is FrameworkElement Then
            Dim fe As FrameworkElement = DirectCast(Me.RootScroller.Content, FrameworkElement)
            fe.Width = Application.Current.Host.Content.ActualWidth
        End If
    End Sub

Cheers
Nathan
File Name Submitted By Submitted On File Size  
Silverlight4GridColumnWidthStarBug.zip 5/18/2010 1.99 MB
Silverlight4GridColumnWidthStarBug.zip 5/21/2010 1.99 MB