如何使用 C# 对齐 ListView 中单个子项的文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/561798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 08:14:42  来源:igfitidea点击:

How do I align text for a single subitem in a ListView using C#?

c#listviewtext-alignment

提问by Fueled

I wasn't able to find an answer anywhere about this seemingly simple topic: is it possible to align text of a single subitem in a WinForms ListView control?

我无法在任何地方找到有关这个看似简单的主题的答案:是否可以在 WinForms ListView 控件中对齐单个子项的文本?

If so, how?

如果是这样,如何?

I would like to have text in the same column aligned differently.

我想让同一列中的文本以不同的方式对齐。

采纳答案by Fueled

For future reference, here's how I solved it:

为了将来参考,这是我解决它的方法:

// Make owner-drawn to be able to give different alignments to single subitems
lvResult.OwnerDraw = true;
...

// Handle DrawSubItem event
private void lvResult_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
{
    // This is the default text alignment
    TextFormatFlags flags = TextFormatFlags.Left;

    // Align text on the right for the subitems after row 11 in the 
    // first column
    if (e.ColumnIndex == 0 && e.Item.Index > 11)
    {
        flags = TextFormatFlags.Right;
    }

    e.DrawText(flags);
}

// Handle DrawColumnHeader event
private void lvResult_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
    // Draw the column header normally
    e.DrawDefault = true;
    e.DrawBackground();
    e.DrawText();
}

It was necessary to handle the DrawColumnHeader, otherwise no text or column separators would be drawn.

必须处理 DrawColumnHeader,否则将不会绘制文本或列分隔符。

回答by David

The "ColumnHeader" class has a "TextAlign" property that will change the alignment for all subitems in the column. If you need something more fancy you could always use the "DrawSubItem" event and make it owner drawn.

“ColumnHeader”类具有“TextAlign”属性,该属性将更改列中所有子项的对齐方式。如果您需要更花哨的东西,您可以随时使用“DrawSubItem”事件并使其由所有者绘制。

回答by Novpiar Effendi

example :

例子 :

listView1.Columns[1].TextAlign = HorizontalAlignment.Right;

will set Column's "1" alignment to right

将列的“1”对齐设置为右侧

回答by Martin.Martinsson

Note: Due to a limitation of the underlying native ListView control (living in comctl32.dll), the first column cannot be aligned. It will be always aligned left. The second limitation is when you custom draw (custom draw subitems): when you enable column reordering, the text of the first column is NOT reordered correctly. I solved this limitation (would not call it a bug, because the listview supports many list styles and the internal data structure of a list view is a tree like one) by not allowing to reorder the first column, which in most cases is no problem, because you'll use some sort of key for the first column like number or something similar.

注意:由于底层原生 ListView 控件(位于 comctl32.dll 中)的限制,第一列无法对齐。它将始终向左对齐。第二个限制是当您自定义绘制(自定义绘制子项)时:当您启用列重新排序时,第一列的文本不会正确重新排序。我通过不允许重新排序第一列来解决这个限制(不会称之为错误,因为列表视图支持许多列表样式,并且列表视图的内部数据结构是一个树状结构),这在大多数情况下是没有问题的,因为您将为第一列使用某种键,例如数字或类似的东西。

回答by Pecilije Velicanstveni

Put the ListView control on form and name it "listV1" and add and edit 4 columns. Also define one Index. Then, format column "0" without the header and with Width=0. That way column 0 will be invisible. Then you can format other columns as you wish (left, center,right) which are all visible.

将 ListView 控件放在窗体上并将其命名为“listV1”并添加和编辑 4 列。还要定义一个索引。然后,格式化列“0”,不带标题,宽度=0。这样第 0 列将不可见。然后您可以根据需要格式化其他列(左、中、右),这些列都可见。

ListView listV1 = new ListView();
uint Index = 0;   
string[] Split_Message = Some_Message.Split(','); // split it to comma...
ListViewItem L = new ListViewItem(""); //... to column "0" assign empty string...
L.SubItems.Add(Index.ToString() + "."); //... than fill the first fild in the row. It is number converted to string...
L.SubItems.Add(Split_Message[0]); //... then first string from buffer...
L.SubItems.Add(Split_Message[1]); //... again, second string from buffer etc. ...
listV1.Items.Add(L); //... and at the end write all of this into the first row.
Indexx++; // Next number.