开发者

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

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.

开发者_如何学Python

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?


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;
}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜