我想更改树视图中所选项目的颜色,即使所选项目没有聚焦。我正在使用 DrawoNode 来实现此目的。但我的 DrawNode 事件没有触发。可能是什么问题呢?这是我的代码:
SolidBrush greenBrush = new SolidBrush(Color.Green);
SolidBrush redBrush = new SolidBrush(Color.Red);
private void tvVouchers_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
if (e.Node.IsSelected)
{
if (tvVouchers.Focused)
e.Graphics.FillRectangle(greenBrush, e.Bounds);
else
e.Graphics.FillRectangle(redBrush, e.Bounds);
}
else
e.Graphics.FillRectangle(Brushes.White, e.Bounds);
e.Graphics.DrawRectangle(SystemPens.Control, e.Bounds);
TextRenderer.DrawText(e.Graphics,
e.Node.Text,
e.Node.TreeView.Font,
e.Node.Bounds,
e.Node.ForeColor);
}
Run Code Online (Sandbox Code Playgroud)
确保将 DrawNode 属性设置为 OwnerDrawText 或 OwnerDrawAll,然后将触发该事件。
OnwerDrawText: 'TreeNode标签区域可以自定义。所有其他 TreeNode 元素都会自动绘制。OwnerDrawAll: '整个TreeNode的外观可以定制。如果需要,必须手动绘制图标、复选框、加号和减号以及连接节点的线。
| 归档时间: |
|
| 查看次数: |
1699 次 |
| 最近记录: |