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

如何清除Xamarin.WPF中ListView的悬停与选中样式?

解决Xamarin.Forms WPF端ListView选中/悬停的蓝色矩形问题

咱们先明确核心问题:你的跨平台项目在Android端的ListView表现符合预期,但WPF端的列表项在悬停或选中时会出现默认的蓝色高亮矩形——这是WPF原生ListViewItem的默认样式导致的。下面给你两种实用的解决方法,你可以根据需求选择:

方法1:全局覆盖WPF项目的ListViewItem样式

如果想让所有ListView都去掉这个蓝色效果,可以在WPF项目的App.xaml中添加自定义样式,直接替换默认的高亮逻辑:

<Application.Resources>
    <Style TargetType="ListViewItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListViewItem">
                    <Border x:Name="Bd"
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            Padding="{TemplateBinding Padding}"
                            SnapsToDevicePixels="true">
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                    </Border>
                    <ControlTemplate.Triggers>
                        <!-- 移除悬停时的高亮背景 -->
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter TargetName="Bd" Property="Background" Value="Transparent" />
                        </Trigger>
                        <!-- 移除选中时的高亮背景与边框 -->
                        <Trigger Property="IsSelected" Value="true">
                            <Setter TargetName="Bd" Property="Background" Value="Transparent" />
                            <Setter TargetName="Bd" Property="BorderBrush" Value="Transparent" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Application.Resources>

这个样式会把所有ListViewItem的悬停、选中背景都设为透明,彻底消除蓝色矩形。

方法2:用自定义渲染器针对单个ListView生效

如果只想让特定的ListView去掉该效果,可以用Xamarin.Forms的自定义渲染器实现:

步骤1:在共享项目中创建自定义ListView类

using Xamarin.Forms;

namespace App1
{
    public class CustomListView : ListView
    {
    }
}

步骤2:在WPF项目中添加对应的渲染器

using App1;
using Xamarin.Forms.Platform.WPF;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

[assembly: ExportRenderer(typeof(CustomListView), typeof(CustomListViewRenderer))]
namespace App1.WPF
{
    public class CustomListViewRenderer : ListViewRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<ListView> e)
        {
            base.OnElementChanged(e);

            if (Control != null)
            {
                var wpfListView = Control as ListView;
                if (wpfListView != null)
                {
                    // 定义ListViewItem的自定义样式
                    var itemStyle = new Style(typeof(ListViewItem));
                    itemStyle.Setters.Add(new Setter(ListViewItem.BackgroundProperty, Brushes.Transparent));
                    itemStyle.Setters.Add(new Setter(ListViewItem.BorderBrushProperty, Brushes.Transparent));

                    // 添加触发器移除悬停高亮
                    var mouseOverTrigger = new Trigger
                    {
                        Property = ListViewItem.IsMouseOverProperty,
                        Value = true
                    };
                    mouseOverTrigger.Setters.Add(new Setter(ListViewItem.BackgroundProperty, Brushes.Transparent));

                    // 添加触发器移除选中高亮
                    var selectedTrigger = new Trigger
                    {
                        Property = ListViewItem.IsSelectedProperty,
                        Value = true
                    };
                    selectedTrigger.Setters.Add(new Setter(ListViewItem.BackgroundProperty, Brushes.Transparent));
                    selectedTrigger.Setters.Add(new Setter(ListViewItem.BorderBrushProperty, Brushes.Transparent));

                    itemStyle.Triggers.Add(mouseOverTrigger);
                    itemStyle.Triggers.Add(selectedTrigger);

                    wpfListView.ItemContainerStyle = itemStyle;
                }
            }
        }
    }
}

步骤3:替换共享项目XAML中的ListView

记得先添加命名空间,再把原ListView换成自定义的CustomListView:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
             xmlns:d="http://xamarin.com/schemas/2014/forms/design" 
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:local="clr-namespace:App1"
             mc:Ignorable="d" 
             x:Class="App1.MainPage">
    <local:CustomListView ItemsSource="{Binding TestList}" SeparatorVisibility="None" HasUnevenRows="True">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <ViewCell.View>
                        <ContentView>
                            <Label Text="{Binding TestName}"></Label>
                        </ContentView>
                    </ViewCell.View>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </local:CustomListView>
</ContentPage>

补充说明

  • 如果你需要保留部分自定义样式(比如选中时换成指定颜色),只需要修改触发器里的Background值即可,比如把Transparent改成你想要的颜色。
  • 方法1全局生效,适合整个项目都需要统一样式的场景;方法2更灵活,只针对特定ListView生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 20:08:11