-
Jan 26th, 2009, 01:15 PM
#1
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...
-tg
-
Jan 26th, 2009, 01:23 PM
#2
Frenzied Member
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.
-
Jan 26th, 2009, 01:31 PM
#3
Frenzied Member
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....
-
Jan 26th, 2009, 01:31 PM
#4
Re: Listbox Selection Color
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.
-
Jan 26th, 2009, 01:35 PM
#5
Frenzied Member
Re: Listbox Selection Color
-
Jan 26th, 2009, 01:38 PM
#6
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)
Code:
<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:
Code:
<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.
Last edited by chris128; Jan 26th, 2009 at 01:41 PM.
-
Jan 26th, 2009, 02:02 PM
#7
Re: Listbox Selection Color
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
-
Jan 26th, 2009, 02:05 PM
#8
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
Last edited by chris128; Jul 2nd, 2009 at 03:35 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|