WPF ListBox选择颜色

时间:2009-04-27 18:49:01

标签: wpf listbox triggers listboxitem

很抱歉,如果以前曾经问过这个问题,但是我无法找到解决方案来解决我在相关问题中找到的问题,或者在Google上找到解决方案。

在我的应用程序中,我正在尝试重新创建单词新文档对话框,列在项目的左侧,右侧是带有文本的图标。在Word中,当鼠标悬停时它具有橙色渐变,而当您选择项目时,它具有较暗的渐变。除了在选择项目后更改背景颜色外,我已经重新创建了大部分内容。这是我用来创建它的代码:

    <ListView Margin="236,34,17,144" Name="listView1" HorizontalContentAlignment="Stretch">
        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid  Columns="5" IsItemsHost="True" VerticalAlignment="Top" >
                </UniformGrid>
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>
        <ListView.ItemTemplate>
            <DataTemplate >
                <StackPanel HorizontalAlignment="Center" Width="auto">
                    <Image Source="images/document32.png" HorizontalAlignment="Center"/>
                    <TextBlock Text="{Binding}" HorizontalAlignment="Center" />
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
        <ListView.ItemContainerStyle>
            <Style TargetType="{x:Type ListViewItem}"  >                 
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="true">
                        <Setter Property="Foreground" Value="Yellow" />
                        <Setter Property="Background" Value="Orange" />
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter Property="Foreground" Value="Black" />
                        <Setter Property="Background">
                            <Setter.Value>
                                <LinearGradientBrush EndPoint="0.5,1" StartPoint="1,0">
                                    <GradientStop Color="#d3e7ff" Offset="0.986"/>
                                    <GradientStop Color="#b0d2fc" Offset="0.5"/>
                                    <GradientStop Color="#8ec1ff" Offset="0.51"/>
                                </LinearGradientBrush>
                            </Setter.Value>
                        </Setter>
                    </Trigger>

                </Style.Triggers>
            </Style>
        </ListView.ItemContainerStyle>
    </ListView>

因此,这会创建我想要的外观,鼠标悬停,当我在列表视图中选择一个项目时,它会将字体文本更改为黄色,但它拒绝将背景从默认蓝色更改为橙​​色,理想情况下,它将是另一个渐变而不是泛滥的颜色。谢谢你的帮助。

1 个答案:

答案 0 :(得分:31)

您可以执行一些黑客操作,例如覆盖系统颜色键,但很可能您需要提供一个新模板来实现此目的。这是我放在一起的相当漂亮的一个:

<Style x:Key="ListboxItemStyle" TargetType="{x:Type ListBoxItem}">
    <Setter Property="Margin" Value="1,2,1,1"/>
    <Setter Property="HorizontalAlignment" Value="Stretch" />
    <Setter Property="Background" Value="{StaticResource NormalItemBackground}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <Grid>
                    <Border Background="{TemplateBinding Background}" />
                    <Border Background="#BEFFFFFF" Margin="3,1">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition />
                                <RowDefinition />
                            </Grid.RowDefinitions>
                            <Border Margin="2,1,2,0" Grid.Row="0" Background="#57FFFFFF" />
                        </Grid>
                    </Border>
                    <ContentPresenter Margin="8,5" />
                </Grid>
                <ControlTemplate.Triggers>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsMouseOver" Value="True" />
                            <Condition Property="IsSelected" Value="False"/> 
                        </MultiTrigger.Conditions>
                        <Setter Property="Background" Value="{StaticResource HotItemBackground}" />
                    </MultiTrigger>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Background" Value="{StaticResource SelectedItemBackground}" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<Style x:Key="ListBoxStyle" TargetType="{x:Type ListBox}">
    <Setter Property="ItemContainerStyle" Value="{DynamicResource ListboxItemStyle}" />
    <Setter Property="Margin" Value="3,3,2,1" />
</Style>