Search
On this page
Archives
RSS 2.0 Categories
Blogroll
Disclaimer
Powered by: newtelligence dasBlog 2.0.7226.0
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
Send mail to the author(s) E-mail
Silverlight UI Rant #1 - DataGrid Last Column Fill#

If you've used the the Silverlight 2 DataGrid, you've no doubt seen this:

silverlightDatagrid01

I really hate when this happens!  I've seen several forum discussions where folks were looking to get rid of this nastiness, so I know I'm not the only one losing sleep over it.  I haven't come across a solution in anything I've seen and so now that I have one, I thought I'd share it. 

I ran into this a few weeks back and tried most of the obvious things with defining the DataGridColumns myself rather than auto-generating them.  In these defined DataGridColumns, I tried setting the Width of the last column to "*" just like you would a standard Grid.ColumnDefinition if you wanted to have it take up the remainder of the space.  This did nothing more than invite my buddy, AG_E_PARSER_BAD_PROPERTY, to show up...again.  It appears that star-sizing isn't yet implemented for DataGridColumn derivatives. 

This morning I was finally able to grab a few minutes to dive into DataGrid and find out what I could do about this.  I decided to create an ExtendedDataGrid and add a LastColumnFill DependencyProperty similar to way the DockPanel has a LastChildFill. Most of the work is happening in this method which is called if the LastColumnFill is true:

private void AdjustLastColumnWidth(Size finalSize)
{
    // get the Vertical ScrollBar
    ScrollBar scrollBar = this.GetTemplateChild("VerticalScrollbar") as ScrollBar;

    // compute the width to allow for the scrollbar based on it's visibility.
    double scrollBarWidthAllowance = (scrollBar != null && scrollBar.Visibility == Visibility.Visible) ? scrollBar.ActualWidth + 2 : 2;

    // compute the width of all the columns excluding the last one
    var widthOfAllButLastColumn = this.Columns
                   .TakeWhile(c => c != Columns.Last() &&
                                            c.Visibility == Visibility.Visible)
                   .Sum(c => c.ActualWidth);

     // set the last column width    
     this.Columns.Last().Width = new DataGridLength(finalSize.Width - widthOfAllButLastColumn - scrollBarWidthAllowance);
}

This is the result:

silverlightDatagrid02

Much better!  The live demo shows it with and without a vertical scrollbar.  Note that it also handles auto-generated DataGridColumns if that's your thang.

Here's the live demo.

Here's the code.

Friday, February 06, 2009 2:15:10 AM (GMT Standard Time, UTC+00:00)
  Comments [0]  | 
Top-10 Application-Design Mistakes#

We saw it when the world first discovered Flash and I'm sure we've got a lot of it left to live through with Silverlight, so this is pretty timely advice from the "Godfather of Web Usability" himself, Jakob Nielsen.  This should be required for every web developer.  There are some great links within the post as well, so be sure to click around.

Top-10 Application-Design Mistakes

Wednesday, February 20, 2008 5:52:10 AM (GMT Standard Time, UTC+00:00)
  Comments [1]  | 
All content © 2010 , Rik Robinson