You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPF ListView样式定制:选中/悬停效果及光标等设置问题

解决WPF ListView的四项样式定制需求

以下是针对你提出的四个样式定制需求的完整实现方案,直接替换原XAML中的ListView相关部分即可:

<ListView
    Grid.Row="1"
    Background="#2b2d30"
    BorderThickness="0"
    ItemsSource="{Binding NotesListingItemViewModel}"
    SelectedItem="{Binding SelectedNotesListingItemViewModel}">
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            <Setter Property="BorderThickness" Value="0" />
            <!-- 移除ALT键触发的虚线焦点轮廓 -->
            <Setter Property="FocusVisualStyle" Value="{x:Null}" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListViewItem">
                        <Border x:Name="Border" Padding="0" Background="Transparent">
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" />
                        </Border>
                        <ControlTemplate.Triggers>
                            <!-- 鼠标悬停时背景设为白色 -->
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="Border" Property="Background" Value="White" />
                            </Trigger>
                            <!-- 选中项(含失去焦点状态)背景设为#d3ddff -->
                            <Trigger Property="IsSelected" Value="True">
                                <Setter TargetName="Border" Property="Background" Value="#d3ddff" />
                            </Trigger>
                            <!-- 确保选中项失去焦点后仍保持指定背景色 -->
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsSelected" Value="True" />
                                    <Condition Property="IsFocused" Value="False" />
                                </MultiTrigger.Conditions>
                                <Setter TargetName="Border" Property="Background" Value="#d3ddff" />
                            </MultiTrigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListView.ItemContainerStyle>
    <ListView.ItemTemplate>
        <DataTemplate>
            <Border Padding="10">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="auto" />
                    </Grid.ColumnDefinitions>

                    <!-- 针对"body 2" TextBox设置白色光标 -->
                    <TextBox Grid.Column="0" Text="{Binding Title}" CaretBrush="White" />

                    <whitedropdownmenucontrol:WhiteDropdownMenu Grid.Column="1">
                        <StackPanel>
                            <Button
                                Width="200"
                                Height="40"
                                Padding="10"
                                Content="MARKED AS COMPLETE" />
                            <Button
                                Width="200"
                                Height="40"
                                Padding="10"
                                Content="MARKED AS IMPORTANT" />
                            <Button
                                Width="200"
                                Height="40"
                                Padding="10"
                                Content="DELETE" />
                        </StackPanel>
                    </whitedropdownmenucontrol:WhiteDropdownMenu>
                </Grid>
            </Border>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

各需求实现说明:

  • 选中项背景色替换为#d3ddff:重写ListViewItem的ControlTemplate,通过IsSelected触发器和IsSelected+IsFocused=False的多触发器,统一设置背景色为指定值,解决默认选中后失焦背景变色的问题。
  • 鼠标悬停项背景设为白色:添加IsMouseOver触发器,直接修改容器Border的背景色为白色。
  • 选中项失焦后保持背景色+TextBox光标白色:多触发器确保选中项即使失去焦点(如点击TextBox)背景色不变;给TextBox添加CaretBrush="White"属性设置光标颜色。
  • 移除ALT键触发的虚线轮廓:给ListViewItem设置FocusVisualStyle="{x:Null}",彻底禁用默认的焦点虚线样式。

内容的提问来源于stack exchange,提问作者Singot Shabu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 19:14:52