Reputation: 2780
Well i have a windows forms application in which i add a couple of listViews in order to hold some data for the user and it looks like this
As you see my form backcolor is black so the list view's grid lines and header white color makes an annoying contrast so after an hour searching without a luck i decided to ask here.
[Question] : How could i edit the colors of the Header & Grid Lines of the list view to match my needs ?
Upvotes: 8
Views: 16110
Reputation: 10152
You can perform that in a DataGrid, but I don't think there's an easy way for ListView, since there are no properties for those lines, unlike DataGrid.
<Style TargetType="{x:Type DataGrid}">
<Setter Property="HorizontalGridLinesBrush" Value="Red"/>
<Setter Property="VerticalGridLinesBrush" Value="Green"/>
</Style>
Put it into application resources or window resources.
Other than that, there's a way to change the border color of each ListViewItem:
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="BorderBrush" Value="Red"/>
</Style>
Upvotes: -1
Reputation: 63317
It looks like that there is not anyone who's interested in customizing a ListView
to support Grid Line Color
. I've tried this one and want to share here. It's not really good by a little flicker (not really much) when you scroll the ListView items. However it's acceptable. I think I lack some knowledge of win32
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 suggestion of Cody Gray, I added code to handle horizontal scrolling. I use GetScrollPos
for simplicity because as recommended by MSDN documentation page, we should use GetScrollInfo
instead.
Upvotes: 13