在ListBox中碰到过几个问题,现在把它写出来:
第一个就是在ListBoxItem中当我用触发器IsSelected和IsMouseOver来设置Background和Foreground的时候,Foreground是可以直接设置的,但是Background的颜色是不会改变的。网上查了下貌似是需要手动更改ListBoxItem的控件模板让其直接使用ListBoxItem的Background属性。如下:
<Style x:Key="itemtemplate" TargetType="ListBoxItem"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ListBoxItem"> <Border BorderThickness="2" BorderBrush="Red" Background="{TemplateBinding Background}"> <ContentPresenter TextBlock.Foreground="{TemplateBinding Foreground}"/> </Border> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <Trigger Property="IsSelected" Value="true"> <Setter Property="Background" Value="Gray"/> <Setter Property="Foreground" Value="White"/> </Trigger> <Trigger Property="IsMouseOver" Value="true"> <Setter Property="Background" Value="LightGreen"/> <Setter Property="Foreground" Value="Red"/> </Trigger> </Style.Triggers> </Style>