.NET ListView,最大字符数或最大列宽?可以覆盖/扩展吗?
我有一个 .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();
}
}
屏幕截图:
如您所见,包含 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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此行为记录在
ListViewItem 的 MSDN 页面
:根据 Microsoft 员工的说法:
还有一篇关于此问题的 Microsoft 支持文章。
ListViewItem
确实存储了全文,它只是长度有限的显示。但是,如果您制作自定义
ListView
并将其设置为OwnerDraw
,则似乎可以显示全文:这将显示以下内容的全文:每个ListViewItem。这样做的缺点是,您还需要自定义绘制其他视觉状态(例如选定状态、焦点状态等),除非您可以以某种方式将它们路由到原始绘制代码。
我不知道这样做是否还有其他副作用。
This behaviour is documented in the MSDN pages for
ListViewItem
:According to a Microsoft employee:
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 toOwnerDraw
: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.