ListBoxItem IsSelected style
I still didn't get it. Could you please show me exactly how to override ListBox's default behavior. Everytime when ListBoxItem is selected the Border's background should be changed. Not the background of the whole row but only background of the border which's specified.
<ListBox ItemsSource="{Binding Source={StaticResource AssetsViewSource}}">
<ListBox.ItemTemplate>
<DataTemplate>
开发者_JAVA技巧<Border BorderThickness="2" BorderBrush="Black">
<StackPanel>
<TextBlock Text="Name: " />
<TextBlock Text="{Binding Name}" />
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Use the DataTemplate's Triggers collection, with a RelativeSource to get you to the containing ListBoxItem:
<DataTemplate>
<Border BorderThickness="2" BorderBrush="Black" Name="Bd">
<StackPanel>
<TextBlock Text="Name: " />
<TextBlock Text="{Binding Name}" />
</StackPanel>
</Border>
<DataTemplate.Triggers>
<DataTrigger Value="True"
Binding="{Binding
IsSelected,
RelativeSource={RelativeSource
AncestorType={x:Type ListBoxItem}}}">
<!-- everybody loves HotPink -->
<Setter TargetName="Bd" Property="Background" Value="HotPink"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
Simply add the following into the ListBox Item tag
<ListBox.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
</ListBox.Resources>
That should do the trick..
精彩评论