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