Поделиться через


Changing the selection color in a ListBox

I just worked through figuring this out today, and since it was sufficiently irritating, I thought I'd post it here. Plus, I don't want all my 'loyal readers' to think I've disappeared :).

Anyway, in the application I'm working on, I have a ListBox with specific elements in it. If it were databound, I know how to change the selection color, but in this case, I couldn't seem to figure out how to get anything except the System Colors.

If you want to see what I mean in Blend, drop a ListBox on the artboard and using the Right Click menu, add a couple ListBoxItem to it. Now, run the app. When you select any of the items, it is highlighted in whatever color your computer has as the SelectedItem color (On my machine, it's Silver, because I'm using XP with a Silver color scheme).

However, digging through the properties of the ListBox, I couldn't find which property was bound to that System Resource. So, how could I override it and change it.

Here are the steps I eventually had to do: (These are the steps from the start)

  1. Create a ListBox
  2. Add a ListBoxItem to the ListBox
  3. Select the ListBoxItem
  4. Edit the Template of the ListBoxItem (Remember the name, you'll need it later)
  5. Now, in the Triggers pane, choose the property trigger for "IsSelected = True"
  6. Select the Bd element
  7. In the Property pane, you will see that next to the Background brush, the property marker (the little square) is green. This is indicating that it's using a resource. Just change the brush type to something else, and the marker will switch back to white. You can now edit the background to whatever you want.

Now, with the IsSelected Property Trigger selected, any changes you make will be put into the Template, and you're good to go. You will probably want to go back to the root (using the scope up button), and assign that Style to all the ListBoxItem, so select them all in the Object Tree, and set the Style property to the local resource that was created in step 4 above.

Comments