Set the color of the ListView control
Are summarized as follows:
The listview of OwnerDraw property is set to true and the View property is set to View. Details , it will trigger
ListViewDrawColumnHeader events (including event ListView DrawItem, ListView DrawSubItem event) Videos own background color, the following code can be separately three column headers (the ColumnHeader) redraw, Item and SubItem not redrawn.
-
private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
-
{
-
if (e.ColumnIndex == 0)
-
{
-
e.Graphics.FillRectangle (Brushes.DarkGray, e.Bounds); // using a specific color to draw the title bar, where I used gray
-
e.DrawText (); // default way to draw the title text
-
}
-
-
else if (e.ColumnIndex == 1)
-
{
-
e.Graphics.FillRectangle (Brushes.DarkGray, e.Bounds); // using a specific color to draw the title bar, where I used gray
-
e.DrawText (); // default way to draw the title text
-
}
-
-
else if (e.ColumnIndex == 2)
-
{
-
e.Graphics.FillRectangle (Brushes.DarkGray, e.Bounds); // using a specific color to draw the title bar, where I used gray
-
e.DrawText (); // default way to draw the title text
-
}
-
}
-
-
private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
-
{
-
= e.DrawDefault to true; // draw the items using the system default
-
}
-
-
private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
-
{
-
= e.DrawDefault to true; // draw the items using the system default
-
}
For SubItem of Item and repaint, see the code below
-
private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
-
{
-
e.Graphics.FillRectangle (Brushes.Red, e.Bounds); // using a specific color to draw the title bar, where I used red
-
e.DrawText (); // default way to draw the title text
-
}
-
-
private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
-
{
-
e.Graphics.FillRectangle (Brushes.Red, e.Bounds); // using a specific color to draw the title bar, where I used red
-
e.DrawText (); // default way to draw the title text
-
}
Header row centered in the listview OwnerDraw the property set to true, the alignment of the title bar of the code would take effect;
If the listview OwnerDraw property is set to false, the alignment of the title bar code will not change (always aligned to the left).
With particular reference to the following code:
-
= this.listView1.OwnerDraw to true; // allow self-drawn.
-
-
ColumnHeader ch = new ColumnHeader();
-
= ch.Text "1 column heading"; // Set the column headings
-
= ch.Width 120; // set the column width
-
= HorizontalAlignment.Center ch.TextAlign; // set the alignment of columns, this.listView1.OwnerDraw = true effective.
-
this.listView1.Columns.Add (CH); // column header to the ListView control.