You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET C#:不调整列宽时如何实现ListView项文本完整换行显示

实现ListView项文本自动换行并自适应行高

你遇到的这个问题我之前也碰过,直接设置ListViewItem.Height会报错是因为这个属性是只读的,没法直接赋值。要实现固定列宽下文本自动换行、行高自适应内容的效果,只需要调整ListView的几个关键属性就行,具体步骤如下:

1. 配置ListView的核心属性

首先要确保你的ListView处于Details视图(因为你用了多列),然后开启文本换行功能,同时固定列宽:

// 假设你的ListView实例名为listViewProducts
listViewProducts.View = View.Details;
// 开启文本自动换行
listViewProducts.LabelWrap = true;
// 添加固定宽度的列,比如Descripcion列宽200,Precio列宽80
listViewProducts.Columns.Add("Descripcion", 200);
listViewProducts.Columns.Add("Precio", 80);
// 可选:开启整行选中,提升交互体验
listViewProducts.FullRowSelect = true;

2. 添加列表项(无需手动设置行高)

配置好属性后,直接添加带长文本的项即可,ListView会自动根据内容换行并调整行高:

// 添加第一个产品项
ListViewItem row1 = new ListViewItem("All the description in one single row");
row1.SubItems.Add("80");
listViewProducts.Items.Add(row1);

// 添加第二个带更长描述的产品项
ListViewItem row2 = new ListViewItem("Another item in one single row with even longer text that will wrap automatically within the fixed column width");
row2.SubItems.Add("70");
listViewProducts.Items.Add(row2);

关键说明

  • 为什么之前设置row.Height会报错?因为ListViewItem的Height属性是只读的,控件会根据内容自动计算行高,不需要手动赋值。
  • 如果需要统一设置所有行的固定高度(而非自适应),可以用ListView.ItemHeight属性,但这不符合你要的内容自适应需求。

内容的提问来源于stack exchange,提问作者Javier Salas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:27:33