Changing ListView and Grid Lines Header Colors

Well, I have a Windows forms application in which I add a listView pair to store some data for the user, and it looks like

enter image description here

As you can see, my inverse feature of the form is black, so the list grid lines and the white color of the title bar make an annoying contrast, so after an hour of searching without luck I decided to ask here.

[Question]: How can I change the colors of the title bar and list grid according to my needs?

+7
c # winforms
source share
2 answers

You can accomplish this in a DataGrid, but I don't think this is an easy way for a ListView, since there are no properties for these rows, unlike a DataGrid.

<Style TargetType="{x:Type DataGrid}"> <Setter Property="HorizontalGridLinesBrush" Value="Red"/> <Setter Property="VerticalGridLinesBrush" Value="Green"/> </Style> 

Put it in application resources or window resources.

In addition, there is a way to change the border color of each ListViewItem element:

 <Style TargetType="{x:Type ListViewItem}"> <Setter Property="BorderBrush" Value="Red"/> </Style> 
-one
source share

Nobody seems to be interested in setting up a ListView to support Grid Line Color . I tried this one and want to share it here. This is not very good with a little flicker (not very much) when you are viewing ListView items. However, this is acceptable. I think I lack win32 knowledge here to make it more perfect:

 public class CustomListView : ListView { bool scrollDown; int lastScroll; public Color GridLinesColor {get;set;} [DllImport("user32")] private static extern int GetScrollPos(IntPtr hwnd, int nBar); public CustomListView(){ GridLinesColor = Color.Red; DoubleBuffered = true; base.GridLines = false;//We should prevent the default drawing of gridlines. } public new bool GridLines {get;set;} protected override void WndProc(ref Message m) { if (m.Msg == 0x20a){//WM_MOUSEWHEEL = 0x20a scrollDown = (m.WParam.ToInt64() >> 16) < 0; } if (m.Msg == 0x115){//WM_VSCROLL = 0x115 int n = (m.WParam.ToInt32() >> 16); scrollDown = n > lastScroll; lastScroll = n; } base.WndProc(ref m); if (m.Msg == 0xf && GridLines && Items.Count > 0&&View==View.Details)//WM_PAINT = 0xf { using (Graphics g = CreateGraphics()) { using(Pen p = new Pen(GridLinesColor)){ int w = -GetScrollPos(Handle, 0); for (int i = 0; i < Columns.Count; i++) { w += Columns[i].Width; g.DrawLine(p, new Point(w, 0), new Point(w, ClientSize.Height)); } int a = Items[0].Bounds.Bottom - 1; int b = Height - Items[0].Bounds.Y; int c = Items[0].Bounds.Height; for (int i = scrollDown ? a + (b/c) * c : a ; scrollDown ? i >= a : i < b ; i += scrollDown ? -c : c) { g.DrawLine(p, new Point(0, i), new Point(ClientSize.Width, i)); } } } } } } 

UPDATE:. Thanks to Cody Gray's suggestion, I added code to handle horizontal scrolling. I use GetScrollPos for simplicity because, as recommended on the MSDN documentation page, GetScrollInfo should be used GetScrollInfo .

enter image description here

+6
source share

All Articles