Click to See Complete Forum and Search --> : Listbox Selection Color
techgnome
Jan 26th, 2009, 01:15 PM
Ok, so I've been tinkering with the Listbox WPF demo I've been working on, and I was able to change the colors of the items in the list box, so that it kind of looks like a "card" where the title line is colored differently from the reast, and gets a square around the whole thing. In an effort to make it readable (the black on blue doesn't have enough contrast) I changed the forecolor of the title bit to white... Great... OK, only now, when the item gets selected, the whole item goes invisible - it's because the default windows colors get applied to the item for selections.... anyone know how to override the colors used for selection? It looks kind of crappy at the moment like that...
-tg
DeanMc
Jan 26th, 2009, 01:23 PM
As far as I know to override colours you need to apply a new style to the selected item, im not going to lie I haven't played with styles much but Il mess around with them in blend tonight and see what I come up with.
DeanMc
Jan 26th, 2009, 01:31 PM
Ok it seems that when you either A: Apply a group style or B: override the default style the list item disappears, I imagine this is because there is no style associated with the list item it renders itself lookless....
chris128
Jan 26th, 2009, 01:31 PM
You have to create a new brush that overrides the existing SelectionColor brush I believe. One minute and I'll dig out the code I used.
DeanMc
Jan 26th, 2009, 01:35 PM
et viola, http://www.uxpassion.com/2008/09/styling-wpf-listbox-highlight-color/
chris128
Jan 26th, 2009, 01:38 PM
OK here we go, stick this inside your <Window.Resources> tag in the XAML code. (if you dont already have that tag then just add one right at the top of the XAML, just after where the Window and the XAML schema's etc are declared)
<Style x:Key="TransparentHighlightStyle" TargetType="ListBoxItem">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
</Style.Resources>
<!---Rest of your Style Triggers/Setters etc go here-->
</Style>
So now that we have defined the Style, we can use it like this when we declare a listbox:
<ListBox ItemContainerStyle="{StaticResource TransparentHighlightStyle}" />
Obviously your listbox will have more attributes than that but yeah, its the ItemContainerStyle attribute you need to set.
EDIT: Orrrr if you have blend then you can do it Dean's way :) I prefer to do simple bits and bobs like this in the XAML code myself though, as you end up with less XAML code to look at usually compared to when Blend generates it. But hey, whatever works better for ya.
techgnome
Jan 26th, 2009, 02:02 PM
Aaaaah... okay, that's the XAML way to override the paint event basically.... OK... makes total sense now that I see it. I'll give it a try later and let you know what works.
-tg
chris128
Jan 26th, 2009, 02:05 PM
Well yeah kinda, Styles let you modify the appearance of pretty much any part of a control's appearance so I suppose they are comparable to the Paint event but more powerful as you can include Triggers in them so that certain properties are only changed when a certain condition is met. You can include various other things in them too but you probably already know that :)
EDIT: I have now found that Dean's method is better as the method I posted where you override one of the system brushes can cause problems when you have that same colour somewhere in your listbox items
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.