在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>
View Code

相关文章:

  • 2022-03-02
  • 2022-12-23
  • 2022-12-23
  • 2021-10-16
  • 2022-02-10
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-22
  • 2022-12-23
  • 2021-12-31
  • 2022-12-23
  • 2022-12-23
  • 2021-08-12
相关资源
相似解决方案