我有以下方法为 ListView 的列标题设置背景颜色和字体,但它不起作用。任何人都可以帮助我吗?
private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
using (StringFormat sf = new StringFormat())
{
// Store the column text alignment, letting it default
// to Left if it has not been set to Center or Right.
switch (e.Header.TextAlign)
{
case HorizontalAlignment.Center:
sf.Alignment = StringAlignment.Center;
break;
case HorizontalAlignment.Right:
sf.Alignment = StringAlignment.Far;
break;
}
// Draw the standard header background.
e.DrawBackground();
// Draw the header text.
using (Font headerFont = new Font("Helvetica", 25, FontStyle.Bold)) //Font size!!!!
{
e.Graphics.DrawString(e.Header.Text, headerFont, Brushes.Black, e.Bounds, sf);
}
}
return;
}
不管我用不用这个方法。它保持不变。没有什么变化。 是因为 ListView 的属性吗?
最佳答案
您的代码看起来没问题。如果它没有执行任何操作,则您忘记将 OwnerDraw
属性设置为 true
。
这也意味着您需要为另外两个绘制事件添加代码。默认操作将执行:
private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
{
e.DrawDefault = true;
}
private void listView1_DrawSubItem(object sender, DrawListViewItemEventArgs e)
{
e.DrawDefault = true;
}
别忘了 hook up事件;-)
关于c# - ListView DrawColumnHeader 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33859446/