ListView 列是否有可显示的最大长度?
我遇到了一个奇怪的 ListView 问题,我会尽力解释它。我有一个包含 4 列的 ListView,最后一列是不同长度的消息字符串。我有一些功能,如果 ListView 项目包含某些关键字(失败、异常等),则会将其更改为红色。
当一个项目是红色的并且我在列中没有看到任何会触发红色着色代码的单词时,我第一次注意到这个问题。因此,我将传入字符串的长度添加到该项目之前,并添加了一个文本框,该文本框将在选择时显示该列的文本。我发现前置长度(传入字符串的实际长度)类似于 953,提取的 ListViewItem 的文本长度将为 960(str 长度 + 前置长度信息),但文本框长度中的文本为 253 ……
这是怎么回事?就像所有文本都已放入 ListViewItem 中,但它不能/不会显示全部内容(不,它不是列宽,在上述情况下我将其设置为超过 1000)。
添加 ListViewItem 并检查错误字符串:
ListViewItem listItem = new ListViewItem(msg.Date);
// Add sub-items for Details view.
listItem.SubItems.Add(msg.Time);
listItem.SubItems.Add(msg.Thread);
listItem.SubItems.Add("L: " + msg.Message.Length + " " + msg.Message);
if (!msg.Message.Contains("FA_FAILCNT"))
{
if (msg.Message.Contains("fail", StringComparison.OrdinalIgnoreCase) ||
msg.Message.Contains("exception", StringComparison.OrdinalIgnoreCase) ||
msg.Message.Contains("db q", StringComparison.OrdinalIgnoreCase))
{
listItem.Font = new Font(listItem.Font, FontStyle.Bold);
listItem.ForeColor = Color.Red;
}
else
listItem.ForeColor = Color.Black;
}
显然,它是给我带来问题的最后一个子项目(获取 msg.Message 的那个子项目)
编辑:好吧,废话,这解释了....有什么办法解决这个问题吗?
I'm encountering a strange ListView problem and will do my best to explain it. I have a ListView with 4 columns, the last one being a message string of varying length. I have some functionality that will change the ListView item red if it contains certain keywords (fail, exception, ect).
I first noticed this issue when one item was red and I didn’t see any word in the column that would trigger the red coloring code. So, I had the Length of the incoming string prepending to the item and added a textbox that would display that column's text when selected. What I found was that the prepending length (actual length of incoming string) would be like 953, the extracted ListViewItem's Text length would be 960 (str length + prepended length info), but the text that would be in the text box's length was 253...
What's going on here? Its like all the text made it into the ListViewItem but it can't/won't show it all (and no, its not column width, I had it set to over 1000 in the above case).
Adding the ListViewItem and checking for error strings:
ListViewItem listItem = new ListViewItem(msg.Date);
// Add sub-items for Details view.
listItem.SubItems.Add(msg.Time);
listItem.SubItems.Add(msg.Thread);
listItem.SubItems.Add("L: " + msg.Message.Length + " " + msg.Message);
if (!msg.Message.Contains("FA_FAILCNT"))
{
if (msg.Message.Contains("fail", StringComparison.OrdinalIgnoreCase) ||
msg.Message.Contains("exception", StringComparison.OrdinalIgnoreCase) ||
msg.Message.Contains("db q", StringComparison.OrdinalIgnoreCase))
{
listItem.Font = new Font(listItem.Font, FontStyle.Bold);
listItem.ForeColor = Color.Red;
}
else
listItem.ForeColor = Color.Black;
}
Obviously its the last subitem thats giving me the issues (the one that gets msg.Message)
EDIT: Well crap, this explains it.... any ways around this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已经找到了未显示所有文本的原因。
到目前为止,我发现的最佳解决方案是将信息放在工具提示中,以便当用户将鼠标悬停在该列上时可以看到整个字符串 - 请参阅 Listview 子项文本为尽管文本长度正确,但未在 UI 中完全显示。
我看到这项工作的另一种方式是允许用户复制单元格的值。尽管显示的文本被截断,但通过将单元格值复制并粘贴到另一个应用程序中,您可以查看完整文本。
我想唯一的其他“解决方法”将涉及编写您自己的控件 - 或者我不认为 WPF 中的 ListView 控件具有相同的限制。
You have already found the reason why not all of the text is displayed.
The best solution that I've found so far is to put the information in the tooltip so that the entire string is visible when the user hovers their mouse over the column - see Listview subitem text is not shown fully in the UI eventhough the length of the text is correct.
Another way I've seen this work is allowing users to copy the value of a cell. Although the displayed text is truncated, copying and pasting the cell value into another application allows you to view the full text.
I imagine that the only other "workaround" would involve writing your own control - alternatively I don't think the ListView control in WPF has this same limitation.