如何用image& amp替换子项文本?只在列表视图中显示图像?

时间:2016-07-08 19:43:30

标签: c# listview

我需要再次提供一些帮助。我已经成功地完成了它,但我遇到了问题

任务:在列表视图中,我有行,并且某些列显示true或false。我想用图像替换真假文本来表示我已完成该部分,但图像显示但是真或假的文本仍在显示,我尝试的任何内容都不允许我删除它们而不会引起其他问题

我将提供截图以及listview1_DrawSubItem的代码

Picture of what it does

以下是Draw SubItem的代码

 private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
    {

        if (e.Header == this.columnHeader9)
        {



            if (e.Item.SubItems[8].Text == "True")
            {
                var imageRect = new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Height, e.Bounds.Height);
                e.Graphics.DrawImage(imageList.Images[8], imageRect);
                                }

            if (e.Item.SubItems[8].Text == "False")
            {
                var imageRect = new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Height, e.Bounds.Height);
                e.Graphics.DrawImage(imageList.Images[7], imageRect);
            }




        }
        if (e.Header == this.columnHeader11)
        {
            if (e.Item.SubItems[9].Text == "True")
            {
                var imageRect = new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Height, e.Bounds.Height);
                e.Graphics.DrawImage(imageList.Images[7], imageRect);
                                 }

            if (e.Item.SubItems[9].Text == "False")
            {
                var imageRect = new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Height, e.Bounds.Height);
                e.Graphics.DrawImage(imageList.Images[8], imageRect);
                                }
                         }
        if (e.Header == this.columnHeader13)
        {
            if (e.Item.SubItems[11].Text == "True")
            {
                var imageRect = new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Height, e.Bounds.Height);
                e.Graphics.DrawImage(imageList.Images[7], imageRect);
                                 }
            if (e.Item.SubItems[11].Text == "False")
            {
                var imageRect = new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Height, e.Bounds.Height);
                e.Graphics.DrawImage(imageList.Images[8], imageRect);
                                 }
                       }
        if (e.Header == this.columnHeader14)
        {
            if (e.Item.SubItems[12].Text == "True")
            {
                var imageRect = new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Height, e.Bounds.Height);
                e.Graphics.DrawImage(imageList.Images[7], imageRect);
                                }
            if (e.Item.SubItems[12].Text == "False")
            {
                var imageRect = new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Height, e.Bounds.Height);
                e.Graphics.DrawImage(imageList.Images[8], imageRect);
                                }
                       }

        if (e.Header == this.columnHeader20)
        {
            if (e.Item.SubItems[19].Text == "True")
            {
                var imageRect = new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Height, e.Bounds.Height);
                e.Graphics.DrawImage(imageList.Images[7], imageRect);
                                }
            if (e.Item.SubItems[19].Text == "False")
            {
                var imageRect = new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Height, e.Bounds.Height);
                e.Graphics.DrawImage(imageList.Images[8], imageRect);
                                }
                         }
        e.DrawText();
    }

我希望有人可以帮助我做到这一点,因为这一切都已完成但是这个问题。

1 个答案:

答案 0 :(得分:0)

我好像得到了它。我添加了这个

    if (e.Header != this.columnHeader9 &
     e.Header != this.columnHeader11 &  e.Header != this.columnHeader13
     & e.Header != this.columnHeader14 & e.Header != this.columnHeader20)
        {
            e.DrawText();
        }

它有效。 它在所有列上绘制文本,但是我在上面定义了这些列。

感谢您的帮助