WPF ListBox的ListboxItem自定义样式

下图是完成效果:

代码如下:

<Window x:Class="ListBoxScrollTest.MainWindow" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ListBoxScrollTest" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListBox>
            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Setter Property="OverridesDefaultStyle" Value="True" />
                    <Setter Property="SnapsToDevicePixels" Value="True" />
                    <Setter Property="Foreground" Value="Black" />
                    <Setter Property="Height" Value="40" />
                    <Setter Property="FontSize" Value="16" />
                    <Setter Property="VerticalContentAlignment" Value="Center" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                                <Border x:Name="border" CornerRadius="0" Margin="2" BorderBrush="Black"
                                        BorderThickness="0,0,0,0.2">
                                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                            Margin="10,0,0,0" />
                                </Border>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsSelected" Value="True">
                                        <Setter Property="Foreground" Value="#FF46679A" />
                                        <Setter TargetName="border" Property="Background" Value="white" />
                                        <Setter TargetName="border" Property="BorderBrush" Value="#FF46679A" />
                                        <Setter TargetName="border" Property="BorderThickness" Value="4,0,0,0.5" />
                                    </Trigger>
                                    <MultiTrigger>
                                        <MultiTrigger.Conditions>
                                            <Condition Property="IsMouseOver" Value="True" />
                                            <Condition Property="IsSelected" Value="False" />
                                        </MultiTrigger.Conditions>
                                        <MultiTrigger.Setters>
                                            <Setter Property="Foreground" Value="#FF46679A" />
                                            <Setter TargetName="border" Property="Background" Value="white" />
                                            <Setter TargetName="border" Property="BorderBrush" Value="#FF46679A" />
                                            <Setter TargetName="border" Property="BorderThickness" Value="0,0,0,0.5" />
                                        </MultiTrigger.Setters>
                                    </MultiTrigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListBox.ItemContainerStyle>
            <ListBoxItem>简单学习网</ListBoxItem>
            <ListBoxItem>腾讯游戏</ListBoxItem>
            <ListBoxItem>网易游戏</ListBoxItem>
            <ListBoxItem>哔哩哔哩</ListBoxItem>
            <ListBoxItem>敦刻尔克</ListBoxItem>
            <ListBoxItem>必应搜索</ListBoxItem>
            <ListBoxItem>京东商城</ListBoxItem>
        </ListBox>
    </Grid>
</Window>

 

转载于:https://my.oschina.net/u/2525682/blog/1621332

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值