To set the style when an item is selected or not all you need to do is restore the parent ListBoxItem to <DataTemplate> and change the startup style when its IsSelected . For example, the code below will create a TextBlock with the default Foreground color green . Now, if an element is selected, the font will turn red , and when the mouse is finished, the element will turn yellow . This way, you do not need to specify separate data patterns, as suggested in the other answers for each state that you want to slightly change.
<DataTemplate x:Key="SimpleDataTemplate"> <TextBlock Text="{Binding}"> <TextBlock.Style> <Style> <Setter Property="TextBlock.Foreground" Value="Green"/> <Style.Triggers> <DataTrigger Binding="{Binding Path=IsSelected, RelativeSource={ RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem }}}" Value="True"> <Setter Property="TextBlock.Foreground" Value="Red"/> </DataTrigger> <DataTrigger Binding="{Binding Path=IsMouseOver, RelativeSource={ RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem }}}" Value="True"> <Setter Property="TextBlock.Foreground" Value="Yellow"/> </DataTrigger> </Style.Triggers> </Style> </TextBlock.Style> </TextBlock> </DataTemplate>
Darien Pardinas Dec 23 '14 at 4:48 2014-12-23 04:48
source share