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

如何移除.NET MAUI ListViewItem的焦点视觉效果?

解决.NET MAUI WinUI平台ListView项焦点边框问题

以下是针对WinUI平台有效移除或自定义ListView项焦点视觉效果的方法:

方法1:全局覆盖系统资源与样式

在WinUI项目的App.xaml中添加以下资源,直接覆盖WinUI内部控制ListViewItem焦点样式的默认配置:

<maui:MauiWinUIApplication.Resources>
    <!-- 禁用系统默认焦点视觉 -->
    <x:Boolean x:Key="ListViewItemUseSystemFocusVisuals">False</x:Boolean>
    <!-- 移除选中状态的边框厚度 -->
    <Thickness x:Key="ListViewItemSelectedBorderThemeThickness">0</Thickness>
    <!-- 关闭选中指示器 -->
    <x:Boolean x:Key="ListViewItemSelectionIndicatorVisualEnabled">False</x:Boolean>
    
    <!-- 基于默认样式覆盖ListViewItem属性 -->
    <Style TargetType="ListViewItem" BasedOn="{StaticResource DefaultListViewItemStyle}">
        <Setter Property="UseSystemFocusVisuals" Value="False"/>
        <Setter Property="FocusVisualPrimaryBrush" Value="Transparent"/>
        <Setter Property="FocusVisualSecondaryBrush" Value="Transparent"/>
        <Setter Property="FocusVisualPrimaryThickness" Value="0"/>
        <Setter Property="FocusVisualSecondaryThickness" Value="0"/>
    </Style>
</maui:MauiWinUIApplication.Resources>

注意必须通过BasedOn继承默认样式,才能确保属性设置正确覆盖原有配置。

方法2:自定义ListViewItem模板

若上述方法无效,可直接修改控件模板,彻底移除焦点相关视觉元素:

<maui:MauiWinUIApplication.Resources>
    <Style TargetType="ListViewItem" x:Key="CustomListViewItemStyle" BasedOn="{StaticResource DefaultListViewItemStyle}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListViewItem">
                    <ListViewItemPresenter
                        UseSystemFocusVisuals="False"
                        FocusVisualPrimaryThickness="0"
                        FocusVisualSecondaryThickness="0"
                        SelectionCheckMarkVisualEnabled="False"
                        ContentTransitions="{TemplateBinding ContentTransitions}"
                        PointerOverBackground="{ThemeResource ListViewItemPointerOverBackground}"
                        SelectedBackground="{ThemeResource ListViewItemSelectedBackground}"
                        HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                        VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                        ContentMargin="{TemplateBinding Padding}"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <!-- 全局应用自定义模板 -->
    <Style TargetType="ListViewItem" BasedOn="{StaticResource CustomListViewItemStyle}"/>
</maui:MauiWinUIApplication.Resources>

通过直接修改ListViewItemPresenter的属性,完全屏蔽焦点视觉的渲染。

方法3:针对单个ListView设置

若无需全局修改,可在目标ListView的XAML中直接指定ItemStyle:

<ListView>
    <ListView.ItemStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="UseSystemFocusVisuals" Value="False"/>
            <Setter Property="FocusVisualPrimaryThickness" Value="0"/>
            <Setter Property="FocusVisualSecondaryThickness" Value="0"/>
        </Style>
    </ListView.ItemStyle>
    <!-- ListView内容定义 -->
</ListView>

自定义焦点视觉(保留指示器时)

如果仅需修改样式而非移除,可调整焦点相关的画笔与厚度属性:

<Style TargetType="ListViewItem" BasedOn="{StaticResource DefaultListViewItemStyle}">
    <Setter Property="FocusVisualPrimaryBrush" Value="#FF4080"/>
    <Setter Property="FocusVisualSecondaryBrush" Value="#FF2040"/>
    <Setter Property="FocusVisualPrimaryThickness" Value="2"/>
    <Setter Property="FocusVisualSecondaryThickness" Value="1"/>
</Style>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 22:17:33