.NET ListView, max number of characters, or maximum column width? Possible to override/expand?

时光毁灭记忆、已成空白 提交于 2019-11-27 06:16:39

问题


I have a .NET ListView control in which I display stack traces. I used the ListView since I needed to manipulate the font/colors of certain lines.

However, it seems there is some kind of maximum regarding the width of the columns, either the number of characters displayed, or the number of pixels a column can be.

Here is a simple LINQPad example that shows the problem:

void Main()
{
    using (var fm = new Form())
    {
        ListView lv = new ListView();
        fm.Controls.Add(lv);
        lv.Dock = DockStyle.Fill;
        lv.View = View.Details;
        lv.Columns.Add("C", -1, HorizontalAlignment.Left);

        string line = new string('W', 258) + "x";
        lv.Items.Add(line);
        line = new string('W', 259) + "x";
        lv.Items.Add(line);

        lv.AutoResizeColumn(0, ColumnHeaderAutoResizeStyle.ColumnContent);
        lv.Columns[0].Width.Dump();

        fm.ShowDialog();
    }
}

Screenshot:

As you can see, the line containing 258 W's + an X, shows the x, whereas the next line containing one additional W, does not show the x.

The output of the width calculation there shows that the current width of the column is 2864 pixels.

The question is this: Is there anything I can tweak on the ListView to work around this limitation?


回答1:


This behaviour is documented in the MSDN pages for ListViewItem:

The text of the ListViewItem should not exceed 259 characters or unexpected behavior could occur.

According to a Microsoft employee:

Why the length of the text of listview item is limited to 259 character is because that the listview is design for display collection of objects like files and not for general purpose control. So it is similar to the filename length limitation in the windows file system's MAX_PATH.

There is also a Microsoft Support article about this. The ListViewItem does store the full text, it is just the display that is limited in length.


However, it does appear possible to display the full text if you make a custom ListView and set it to OwnerDraw:

public class MyListView : ListView
{
    public MyListView()
    {
        OwnerDraw = true;
        DrawItem += new DrawListViewItemEventHandler(MyListView_DrawItem);
    }

    private void MyListView_DrawItem(object sender, DrawListViewItemEventArgs e)
    {
        e.Graphics.DrawString(e.Item.Text, e.Item.Font, 
                                    new SolidBrush(e.Item.ForeColor), e.Bounds);
    }
}

This displays the full text of each ListViewItem. The disadvantage in doing this is that you will also need to custom draw the other visual states as well (e.g. selected state, focus state, etc...) unless you can somehow route them through to the original drawing code.

I have no idea if there are any other side effects to doing this.



来源:https://stackoverflow.com/questions/5559704/net-listview-max-number-of-characters-or-maximum-column-width-possible-to-ov

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!