改变ListBoxItem选中的颜色

改变ListBoxItem主要是改变的style

 

下面直接看代码吧!!!

 1   <Style TargetType="{x:Type ListBoxItem}">
 2              <Setter Property="SnapsToDevicePixels" Value="true"/>
 3              <Setter Property="OverridesDefaultStyle" Value="true"/>
 4              <Setter Property="Template">
 5                  <Setter.Value>
 6                      <ControlTemplate TargetType="{x:Type ListBoxItem}">
 7  
 8                          <Grid SnapsToDevicePixels="true">
 9                              <Border x:Name="Border" Background="Aqua"/>
10                              <ContentPresenter />
11                          </Grid>
12                          <ControlTemplate.Triggers>
13                              <Trigger Property="IsSelected" Value="true">
14                                  <Setter Property="Background" TargetName="Border" Value="Red"/>
15                              </Trigger>
16                          </ControlTemplate.Triggers>
17                      </ControlTemplate>
18                  </Setter.Value>
19              </Setter>
20          </Style>

 

在第一行代码要注意的是

<Style TargetType="{x:Type ListBoxItem}">这里没有使用x:key属性

这样模块下所有的ListBoxItem

总结:当ListBoxItem中指定了style时,就不会使用到以上定义的样式。

根据上文指定了ListBoxItem 自动填充ListBox
 <ListBox.ItemContainerStyle>
3     <Style TargetType="ListBoxItem">
4       <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
5     </Style>
6   </ListBox.ItemContainerStyle>

 


可以把上面的样式写在这里,但是这两个样式不能同使用。

 

转载于:https://www.cnblogs.com/qq247039968/p/4021154.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值