我希望像这样对齐我的Listview 列标题:
图例:(C)=中心对齐,(L)=左对齐
(L) Column Header 1 | (C) Header 2 | (C) Header 3 | (L) Header 4
列表视图中的所有项然后左对齐(L)
我在添加/编辑列时意识到.TextAlign
属性,但是,对齐的任何内容也会使该列中的项目保持一致(即我设置的列标题为(C) )也会让这些列中的所有项目居中对齐,这是我不想要的。)
这可以吗?
答案 0 :(得分:1)
您可以OwnerDraw
ColumnHeaders
:设置listView1.OwnerDraw = true;
以下是为三个Columns
设置单独路线的示例:
private void listView1_DrawColumnHeader(object sender,
DrawListViewColumnHeaderEventArgs e)
{
ColumnHeader ch = listView1.Columns[e.ColumnIndex];
HorizontalAlignment colHal = ch.TextAlign;
HorizontalAlignment headHal = colHal;
// three headers that use a different alignment than the columns :
if (e.ColumnIndex == 0) headHal = HorizontalAlignment.Center;
if (e.ColumnIndex == 1) headHal = HorizontalAlignment.Right;
if (e.ColumnIndex == 2) headHal = HorizontalAlignment.Left;
SizeF size = e.Graphics.MeasureString(ch.Text, e.Font);
float x =
headHal == HorizontalAlignment.Center ? ( e.Bounds.Width - size.Width ) / 2f :
headHal == HorizontalAlignment.Right ? ( e.Bounds.Width - size.Width ) : 0f;
e.DrawBackground();
using (SolidBrush brush = new SolidBrush(e.ForeColor) )
e.Graphics.DrawString(ch.Text, e.Font, brush,
e.Bounds.X + x, 5f, StringFormat.GenericDefault);
}
private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
{
e.DrawDefault = true;
}
private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
{
e.DrawDefault = true;
}
您可能需要使用5f
垂直偏移量,具体取决于您的字体..