Listbox Selection Color-VBForums
Results 1 to 8 of 8

Thread: Listbox Selection Color

Hybrid View

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    May 2002
    Posts
    25,528

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  2. #2
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,381

    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.

  3. #3
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,381

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

  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,595

    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.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5
    Frenzied Member
    Join Date
    Jul 2008
    Location
    Rep of Ireland
    Posts
    1,381

    Re: Listbox Selection Color


  6. #6
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,595

    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 12:41 PM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  7. #7

    Thread Starter
    PowerPoster
    Join Date
    May 2002
    Posts
    25,528

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,595

    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.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.