In WPF using a ListView is there an elegant way to auto resize items?

Posted by Justin on Stack Overflow See other posts from Stack Overflow or by Justin
Published on 2010-05-24T17:49:25Z Indexed on 2010/05/24 17:51 UTC
Read the original article Hit count: 258

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about wpf