If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Listbox Selection Color

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...

Re: Listbox Selection Color

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.

Re: Listbox Selection Color

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....

Re: Listbox Selection Color

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)

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.

Re: Listbox Selection Color

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