.NET ListView,最大字符数或最大列宽?可以覆盖/扩展吗?

发布于 2024-10-30 13:33:33 字数 991 浏览 3 评论 0原文

我有一个 .NET ListView 控件,在其中显示堆栈跟踪。我使用 ListView 因为我需要操纵某些行的字体/颜色。

然而,似乎列的宽度存在某种最大值,无论是显示的字符数,还是一列的像素数。

这是一个简单的 LINQPad 示例,显示了问题:

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

屏幕截图:

listview 问题的屏幕截图

如您所见,包含 258 个 W + 一个 X 的行显示 x,而包含一个额外 W 的下一行不显示 x。

宽度计算的输出显示该列的当前宽度为 2864 像素。

问题是:我可以在 ListView 上进行任何调整来解决此限制吗?

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:

screenshot of listview problem

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你是年少的欢喜 2024-11-06 13:33:33

此行为记录在 ListViewItem 的 MSDN 页面

ListViewItem 的文本不应超过 259 个字符,否则可能会出现意外行为。

根据 Microsoft 员工的说法

为什么listview项目的文本长度限制为259个字符是
因为列表视图是为显示对象集合而设计的,例如
文件而不是用于通用控制。所以它类似于文件名
Windows 文件系统 MAX_PATH 的长度限制。

还有一篇关于此问题的 Microsoft 支持文章ListViewItem 确实存储了全文,它只是长度有限的显示。


但是,如果您制作自定义ListView并将其设置为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);
    }
}

这将显示以下内容的全文:每个ListViewItem。这样做的缺点是,您还需要自定义绘制其他视觉状态(例如选定状态、焦点状态等),除非您可以以某种方式将它们路由到原始绘制代码。

我不知道这样做是否还有其他副作用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文