0
编辑:我已经想出了为什么文本绘制为粗体。因为第一列调用OnDrawSubItem两次。有人知道为什么ListView刷新不能正常工作
我已经创建了一个继承自ListView类的自定义控件。我已经覆盖OnDrawSubItem和OnDrawItem方法,如下所示:
protected override void OnDrawSubItem(DrawListViewSubItemEventArgs e)
{
Rectangle rowBounds = e.SubItem.Bounds;
Rectangle labelBounds = e.Item.GetBounds(ItemBoundsPortion.Label);
Rectangle bounds = new Rectangle(rowBounds.Left + 5, rowBounds.Top + 2, labelBounds.Width - 5, rowBounds.Height + 2);
if (e.Item.Selected)
e.Graphics.DrawString(e.SubItem.Text, Font, Brushes.White, bounds);
else
e.Graphics.DrawString(e.SubItem.Text, Font, Brushes.Black, bounds);
}
protected override void OnDrawItem(DrawListViewItemEventArgs e)
{
e.DrawBackground();
if (e.Item.Selected)
{
Rectangle rowBounds = e.Bounds;
Rectangle bounds = new Rectangle(0, rowBounds.Top, rowBounds.Width, rowBounds.Height);
e.Graphics.FillRectangle(Brushes.Gray, bounds);
}
}
但是有些东西不能正常工作。当我添加一些项目到ListView时,文本有点厚。列表视图被放置在一个选项卡中。当我切换到另一个选项卡并回到带有listview的选项卡时,文本看起来很好。于是,我就做这一点,并试图在项目增加操作的末尾添加listView.Refresh()
,但没有奏效:
//code 1
foreach(string s in new string[] { "item1", "item2", "item3", "item4", "item5" })
{
listView.Items.Add(s);
}
listView.Refresh();
输出是:
我也尝试刷新在每次迭代,这是一个好一点,但最后一个元素没有被刷新:
//code 2
foreach(string s in new string[] { "item1", "item2", "item3", "item4", "item5" })
{
listView.Items.Add(s);
listView.Refresh();
}
输出:
当切换选项卡时,所有文本都是应该如何处理的。
我的两个问题在这里:
- 为什么加入时,他们的文字渲染不好?
- 代码1和代码2有什么区别?我认为他们应该以同样的方式工作。
另外,也许值得一提的是,如果我添加第二列,第二列呈现良好。
当我点击某件物品时,它删除了文字。 –