WPF ListBoxItem 取消hover高亮,仅显示选中高亮

发布时间 2023-06-16 15:21:41作者: 盛沧海

 

ListBoxItem 取消hover高亮,仅显示选中高亮;其他控件与此类似

 

  <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBoxItem}">
                        <Border Background="{TemplateBinding Background}"
                                BorderThickness="1">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <!--<Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightColor}}"/>
                </Trigger>-->
                <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource Self}}" Value="true">
                    <Setter Property="Background" Value="#FFBFEFFF"/>
                </DataTrigger>

            </Style.Triggers>
        </Style>

上述xml中注释也能实现选中高亮,但是由于其为系统颜色,不太符合测试要求。