Hiding the selection in a WPF ListBox, keeping it in contained controls

1.6k Views Asked by At

I'm using a ListBox to display a list of editable objects whose template contains, among other things, a ComboBox.

I used this common technique to hide the ListBox selection, which is not used for anything:

<ListBox.Resources>
  <Style TargetType="ListBoxItem">
    <Style.Resources>
      <Brush x:Key="{x:Static SystemColors.HighlightBrushKey}">Transparent</Brush>
      <Brush x:Key="{x:Static SystemColors.ControlBrushKey}">Transparent</Brush>

The problem is this messes with the ComboBox dropdown list selection.

I'd like to override these resources again in my template, specifying the original values (SystemColors.HighlightBrush, etc) instead of hardcoding them. How can I do that?

<ListBox.ItemTemplate>
  <DataTemplate DataType="{x:Type SearchService:Criterion}">
    <DataTemplate.Resources>
      <!--I know how to specify a hardcoded brush here,
          but not how to reference one from SystemColors-->
2

There are 2 best solutions below

0
On BEST ANSWER

I used this common technique to hide the ListBox selection, which is not used for anything

If you do not use the selection for anything you should just use an ItemsControl instead.

4
On

You could do:

<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
    Color="{x:Static SystemColors.HighlightColor}" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}"
    Color="{x:Static SystemColors.ControlColor}" />

or

<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
    Color="{DynamicResource {x:Static SystemColors.HighlightColorKey}}" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}"
    Color="{DynamicResource {x:Static SystemColors.ControlColorKey}}" />

To restore the brushes to their default colors.