WPF设置listbox控件鼠标滑过背景和字体变颜色


 1 <ListBox>
 2     <!-- 数据 -->
 3     <ListBoxItem>AAAA</ListBoxItem>
 4     <ListBoxItem>BB</ListBoxItem>
 5     <ListBoxItem>CCCC</ListBoxItem>
 6     <!-- 设置ListBoxItem样式 -->
 7     <ListBox.ItemContainerStyle>
 8         <Style TargetType="ListBoxItem">
 9             <!-- 设置控件模板 -->
10             <Setter Property="Template">
11                 <Setter.Value>
12                     <ControlTemplate TargetType="ListBoxItem">
13                         <Border Background="{TemplateBinding Background}">
14                             <ContentPresenter HorizontalAlignment="{TemplateBindingHorizontalContentAlignment}"
15                                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
16                                              TextBlock.Foreground="{TemplateBinding Foreground}"/>
17                         </Border>
18                     </ControlTemplate>
19                 </Setter.Value>
20             </Setter>
21             <!-- 设置触发器 -->
22             <Style.Triggers>
23                 <Trigger Property="IsSelected" Value="true">
24                     <Setter Property="Background" Value="Black"/>
25                     <Setter Property="Foreground" Value="White"/>
26                 </Trigger>
27                 <Trigger Property="IsMouseOver" Value="true">
28                     <Setter Property="Background" Value="LightGreen"/>
29                     <Setter Property="Foreground" Value="Red"/>
30                 </Trigger>
31             </Style.Triggers>
32         </Style>
33     </ListBox.ItemContainerStyle>
34 </ListBox>

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM