a-f*_*f-a 4 c# listview winforms
我有以下方法来设置列表视图的列标题的背景颜色和字体,但它不起作用。有人可以帮助我吗?
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;
}
Run Code Online (Sandbox Code Playgroud)
我使用或不使用这个方法并不重要。它保持不变。没有什么变化。是因为列表视图的属性吗?
你的代码看起来没问题。如果它没有执行任何操作,则您忘记将OwnerDraw属性设置为true.
这也意味着您需要为其他两个绘制事件添加代码。默认操作将执行:
private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
{
e.DrawDefault = true;
}
private void listView1_DrawSubItem(object sender, DrawListViewItemEventArgs e)
{
e.DrawDefault = true;
}
Run Code Online (Sandbox Code Playgroud)
不要忘记连接事件;-)
| 归档时间: |
|
| 查看次数: |
3182 次 |
| 最近记录: |