WPF ListBox Selection Color

不打扰是莪最后的温柔 提交于 2019-11-29 09:19:19

There are a few hacks you can do like overriding the system color key, but most likely you will want to provide a new template to achieve this. Here's a fairly nice looking one I put together:

<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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!