Conflicting ListView Item Colouring
Items are colored in green on mouse-over and on keyboard focused. How can i return the colouring of the selected items to white while the mouse is over some item?
it's like I have to separated mechanisms.
XAML:
<Window.Resources>
<SolidColorBrush x:Key="ListBorder" Color="Green"/>
<SolidColorBrush x:Key="Brush_ListItem_MO" Color="Green"/>
<SolidColorBrush x:Key="Brush_Tree_Item_Border" Color="Green"/>
<Style TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Grid x:Name="container">
<Grid.ColumnDefinitions>
<ColumnDefinition Wid开发者_如何转开发th="Auto" />
<ColumnDefinition SharedSizeGroup="A" Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Border x:Name="Border" Grid.ColumnSpan="3" Height="35"
HorizontalAlignment="Stretch" />
<TextBlock Text="{TemplateBinding Content}"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" TargetName="Border" Value="{DynamicResource ListBorder}" />
<Setter Property="Background" TargetName="Border" Value="{DynamicResource Brush_ListItem_MO}" />
<Setter Property="BorderBrush" TargetName="Border" Value="{DynamicResource Brush_Tree_Item_Border}" />
<Setter Property="BorderThickness" TargetName="Border" Value="1" />
</Trigger>
<Trigger Property="IsSelected" Value="true">
<Setter Property="BorderBrush" TargetName="Border" Value="{DynamicResource ListBorder}" />
<Setter Property="Background" TargetName="Border" Value="{DynamicResource Brush_ListItem_MO}" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="true" />
<Condition Property="Selector.IsSelectionActive" Value="false" />
</MultiTrigger.Conditions>
<Setter Property="BorderBrush" TargetName="Border" Value="Red" />
<Setter Property="Background" TargetName="Border" Value="Red" />
<Setter Property="BorderBrush" TargetName="Border" Value="{DynamicResource Brush_Tree_Item_Border}" />
<Setter Property="BorderThickness" TargetName="Border" Value="1" />
</MultiTrigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
</Trigger>
<!--fgdfgdfgfdgfdgfg-->
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<ListBox >
<ListBox.Items>
<ListBoxItem>1</ListBoxItem>
<ListBoxItem>2</ListBoxItem>
<ListBoxItem>3</ListBoxItem>
<ListBoxItem>4</ListBoxItem>
<ListBoxItem>5</ListBoxItem>
</ListBox.Items>
</ListBox>
</Grid>
I separated the scenarios using MultiTrigger
. Following are the scenarios:
- Is Mouse Over and Not Selected (Green)
- Is Selected and Not Mouse OVer (Green)
- Is Mouse Over and Selected (Light Green)
XAML:
<Window.Resources>
<SolidColorBrush x:Key="ListBorder"
Color="Green" />
<SolidColorBrush x:Key="Brush_ListItem_MO"
Color="Green" />
<SolidColorBrush x:Key="Brush_Tree_Item_Border"
Color="Green" />
<SolidColorBrush x:Key="Brush_ListItem_MO_Selected"
Color="LightGreen" />
<Style TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Grid x:Name="container">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition SharedSizeGroup="A"
Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Border x:Name="Border"
Grid.ColumnSpan="3"
Height="35"
HorizontalAlignment="Stretch" />
<TextBlock Text="{TemplateBinding Content}" />
</Grid>
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected"
Value="False" />
<Condition Property="IsMouseOver"
Value="True" />
</MultiTrigger.Conditions>
<Setter Property="BorderBrush"
TargetName="Border"
Value="{DynamicResource ListBorder}" />
<Setter Property="Background"
TargetName="Border"
Value="{DynamicResource Brush_ListItem_MO}" />
<Setter Property="BorderBrush"
TargetName="Border"
Value="{DynamicResource Brush_Tree_Item_Border}" />
<Setter Property="BorderThickness"
TargetName="Border"
Value="1" />
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected"
Value="True" />
<Condition Property="IsMouseOver"
Value="False" />
</MultiTrigger.Conditions>
<Setter Property="BorderBrush"
TargetName="Border"
Value="{DynamicResource ListBorder}" />
<Setter Property="Background"
TargetName="Border"
Value="{DynamicResource Brush_ListItem_MO}" />
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected"
Value="True" />
<Condition Property="IsMouseOver"
Value="True" />
</MultiTrigger.Conditions>
<Setter Property="BorderBrush"
TargetName="Border"
Value="{DynamicResource ListBorder}" />
<Setter Property="Background"
TargetName="Border"
Value="{DynamicResource Brush_ListItem_MO_Selected}" />
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected"
Value="true" />
<Condition Property="Selector.IsSelectionActive"
Value="false" />
</MultiTrigger.Conditions>
<Setter Property="BorderBrush"
TargetName="Border"
Value="Red" />
<Setter Property="Background"
TargetName="Border"
Value="Red" />
<Setter Property="BorderBrush"
TargetName="Border"
Value="{DynamicResource Brush_Tree_Item_Border}" />
<Setter Property="BorderThickness"
TargetName="Border"
Value="1" />
</MultiTrigger>
<Trigger Property="IsEnabled"
Value="false">
<Setter Property="Foreground"
Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
</Trigger>
<!--fgdfgdfgfdgfdgfg-->
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<ListBox>
<ListBox.Items>
<ListBoxItem>1</ListBoxItem>
<ListBoxItem>2</ListBoxItem>
<ListBoxItem>3</ListBoxItem>
<ListBoxItem>4</ListBoxItem>
<ListBoxItem>5</ListBoxItem>
</ListBox.Items>
</ListBox>
</Grid>
精彩评论