【问题标题】:ListView custom highlighting Columns in selected RowListView 自定义突出显示选定行中的列
【发布时间】:2014-12-28 14:55:02
【问题描述】:

我使用的 ListView 包含一些用户可以点击的子项目(有些他不能)。这些对于每一行都是相同的。我用某种背景色标记了这些可点击的子项。

但选择整行(和fullrowselect = true)时,突出显示将与所有子项设置相同的backcolor。

我想做的是向用户显示他选择了哪一行,但仍为子项保留不同的背景颜色。我玩了一下哪个所有者绘制了ListView,但找不到这样做的方法。

感谢您的帮助! 汤姆

【问题讨论】:

标签: c# winforms listview


【解决方案1】:

您需要将ListView 设置为OwnerDraw 并使用如下代码:

private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
{
    if ( e.SubItem.Name != "click1") e.DrawDefault = true; 
    else
    {
        if (e.Item.Selected)
              e.Graphics.FillRectangle(Brushes.SeaShell, 
                                       new Rectangle(e.Bounds.Location, e.Bounds.Size));
        else  e.Graphics.FillRectangle(Brushes.WhiteSmoke, 
                                       new Rectangle(e.Bounds.Location, e.Bounds.Size));
        e.DrawText();
    }
}

private void listView1_DrawColumnHeader(object sender, 
                                        DrawListViewColumnHeaderEventArgs e)
{
    e.DrawDefault = true;
}

显然,您不仅会调整两种颜色/画笔,还会调整可点击列的名称和检查..(可能带有e.SubItem.Name.StartsWith("click_"..)

注意需要设置NamesSubItems你想看可点击; eventParms 中没有提供 SubItemIndex。另请注意,您可能需要在代码中指定Name,即在创建/添加ListViewItem.ListViewSubItem 时;我在设计师中设置的名字不知何故没有坚持/工作!另请注意您需要如何在需要时获得ListViewSubItem 的资格!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-31
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-31
    • 1970-01-01
    • 2018-12-29
    相关资源
    最近更新 更多