冲突的ListView项目着色

时间:2011-02-08 14:14:09

标签: wpf triggers styles listviewitem

项目在鼠标悬停和键盘上着色为绿色。当鼠标悬停在某个项目上时,如何将所选项目的颜色返回白色?

就像我必须分离机制一样。

A screenshot of the list

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 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>
                        <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>

1 个答案:

答案 0 :(得分:0)

我使用MultiTrigger分隔了方案。以下是场景:

  • 鼠标悬停且未选中(绿色)
  • 已选中而非鼠标过期(绿色)
  • 鼠标悬停并选中(浅绿色)

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>