tags:

views:

47

answers:

2

I recently wrote, my first, WPF application that has a list of items that are polled from a web-serivce. The items are displayed/data-bound in a ListView via a GridView. A background thread periodically polls the web-serivce and updates the list.

If, say, I had three items initially bound to the ListView that simply display a description and the three descriptions where something like:

- ProjectA
- ProjectB
- ProjectC

Later a new item is added with a description of 'AReallyReallyLongProjectName', I would end up with a list like:

- ProjectA
- ProjectB
- ProjectC
- AReallyR

The GridViewColumn would not update it's width and would subsequently cut off any new items that extended the original width.

I added this bit of code which forces the column to resize, but it just seems a little hacky. (Just seems weird to set a width just to set it back to nothing to force the resize)

if(gridView != null) {
    foreach(var column in gridView.Columns) {
        if (double.IsNaN(column.Width) column.Width = column.ActualWidth;
        column.Width = double.NaN;
    }
}

Is there a better, more elegant solution, to accomplish this same thing?

+1  A: 

This is one apporach. Another way would be to reset the column width manually each time the items in the list update:

private void ResizeGridViewColumn(GridViewColumn column)
{
    if (double.IsNaN(column.Width))
    {
        column.Width = column.ActualWidth;
    }

    column.Width = double.NaN;
}
bitbonk
Your code snippet is doing what I had posted above. I'm only calling it when new items are set to the `DataContext`, so right now it is only being called during an update.Thanks for the link, it seems like that is even more work than what I have though. :( I thought it would be a little easier for something so common.
Justin
A: 

No.. I suppose there isn't a more elegant solution

Justin