WPF ListBox 选中颜色

14

抱歉如果此前已有人问过此问题,但我在相关弹出的问题或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个回答

31

你可以尝试一些Hack方法,比如覆盖系统颜色键,但大多数情况下,您可能会想提供一个新的模板来实现这一点。这是我准备的一个相当不错的模板:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接