0

In relation to a previous question, I have the following code which disables the blue background:

<ListBox Background="Transparent" BorderBrush="Transparent">
    <ListBox.Style>
        <Style>
            <Style.Resources>
                <!-- Background of selected item when focussed -->
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
                <!-- Background of selected item when not focussed -->
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
            </Style.Resources>
        </Style>
    </ListBox.Style>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Border Margin="5" BorderThickness="2" BorderBrush="LightGray" CornerRadius="5">
                <Expander IsExpanded="True" Background="#f7f7f7">
                    <!-- Content -->
                </Expander>
            </Border>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ListBox>

What I'm noticing is that now all things like combo boxes that are in the content also have the same style of transparent selection. What do I need to do to get the selection to only be transparent for the ListBoxItem and not for its contents?

Walter Williams
  • 944
  • 3
  • 11
  • 25

2 Answers2

1

you can set those brushes to their original value in the DataTemplate like so:

          <DataTemplate>
                <DataTemplate.Resources>
                    <!-- Background of selected item when focussed -->
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Blue" />
                    <!-- Background of selected item when not focussed -->
                    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Blue" />
                </DataTemplate.Resources>
                <Border Margin="5" BorderThickness="2" BorderBrush="LightGray" CornerRadius="5">
                    <Expander IsExpanded="True" Background="#f7f7f7">
                        <!-- Content -->

                    </Expander>
                </Border>
            </DataTemplate>
user1064519
  • 2,180
  • 12
  • 13
  • Thank you, that works although I had to hardcode the 'standard' colors of #3399ff and #f0f0f0 respectively. It would be nice to be able to get the original colors from the system. – Walter Williams Apr 26 '13 at 13:50
0

You will have to target ListBoxItem and add to the ListBox Resources

<ListBox>
  <ListBox.Resources>
    <Style TargetType="{x:Type ListBoxItem}">
        <Style.Resources>
            <!-- Background of selected item when focussed -->
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
            <!-- Background of selected item when not focussed -->
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
        </Style.Resources>
    </Style>
  </ListBox.Resources>
</ListBox>
sa_ddam213
  • 42,848
  • 7
  • 101
  • 110
  • Changing ListBox.Style to ListBox.Resources does eliminate the error but the behavior is the same as the original, a ComboBox or DataGrid inherits the transparent style. – Walter Williams Apr 25 '13 at 21:51