2010-03-27 251 views
6

如何更改ListView上的選擇顏色?默認情況下,當用戶選擇一個項目時,它會顯示藍色背景。我想改變它爲深灰色,或者其他... 感謝您的幫助!更改ListView的背景選擇顏色?

+0

這是在WPF或WinForms? – Joel 2010-03-27 18:07:31

回答

1

ObjectListView - WinForm ListView的一個包裝 - 具有可讓您控制所選行的背景和前景顏色的屬性。它使用Obalix建議的技術,但它已經爲你做了很多努力。

所以,用一點點努力,你就可以生產這樣的事情:

alt text

「利好公司」行顯示選擇一個自定義的前景和背景。

+2

'ObjectListView'不是ListView的替代品。現有代碼的一些重構可能需要使用它。 – 2011-08-09 10:45:14

+0

我分析了這個項目。但我認爲這太難學了。 – 2012-11-10 19:01:51

+0

這個答案正是你需要的:http://stackoverflow.com/a/5188225/1317040 – cnlevy 2014-05-04 13:48:58

2

對於WinForms,您必須將OwnerDraw屬性設置爲true,然後使用DrawItemDrawSubItem事件手動繪製項目。

See here爲例。

7

如果你想你的ListView有Windows資源管理器ListView的風格(包括漂亮的外觀,圓滑的邊緣在Win7/Vista的),你可以使用一個小的P/Invoke來實現這一目標:

[DllImport("uxtheme.dll", CharSet = CharSet.Unicode, ExactSpelling = true)] 
internal static extern int SetWindowTheme(IntPtr hWnd, string appName, string partList); 

// You can subclass ListView and override this method 
protected override void OnHandleCreated(EventArgs e) 
{ 
    base.OnHandleCreated(e); 
    SetWindowTheme(this.Handle, "explorer", null); 
} 
+0

也適用於TreeView! +1 – 2011-07-20 03:27:26