I have a ListBox
with ListBoxItem
s that contains a nested ListBox
. Now I want the top-level ListBox
's items to not have a blue background then they are selected (see pic).
Example Image http://img43.imageshack.us/img43/237/window1.png
I tried to use the XAML below with a style to change the background color to transparent as descibed in this blog post. This works, but it also changes the selected background color of the items in the nested ListBox
. How do I change it to only apply to the top-level items?
<Window x:Class="WpfTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="350" Width="352">
<Grid>
<Grid.Resources>
<DataTemplate x:Key="SampleItemTemplate">
<Border Margin="5" Background="Gray" CornerRadius="3">
<Border.BitmapEffect>
<DropShadowBitmapEffect/>
</Border.BitmapEffect>
<StackPanel Margin="2">
<TextBlock Text="{Binding Path=Lid, StringFormat='ProvID: {0}'}"/>
<ListBox ItemsSource="{Binding Path=TestOrders}"/>
</StackPanel>
</Border>
</DataTemplate>
</Grid.Resources>
<Button Height="23" Name="button1" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="75" Margin="0,0,12,12" Click="button1_Click">Button</Button>
<ListBox Name="listBox1" Margin="12,12,12,41" ItemsSource="{Binding}" ItemTemplate="{StaticResource SampleItemTemplate}">
<ListBox.Resources>
<Style TargetType="{x:Type ListBoxItem}">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/>
</Style.Resources>
</Style>
</ListBox.Resources>
</ListBox>
</Grid>
</Window>
I understand that this style applies to all objects of type ListBoxItem
, but how do I get it to apply only to items in listbox1?
<Style TargetType="{x:Type ListBoxItem}">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/>
</Style.Resources>
</Style>
Probably there is a better way to accomplish what I want, in that case, please tell me!
Do you need both the "inner" and "outer" items to be selectable? I would probably stick with ItemsControl for the outer list and just let the inner items be selectable. Then you could style ListBoxItem without affecting the "outer" items.
I agree with Matt (looks like you can just stick an
ItemsControl
inside aScrollViewer
for the main list), but you can also reset the colors back to their defaults inside the scope for the childListBox
es: