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

.NET MAUI中如何修改ListView项鼠标悬停时的背景色

修改.NET MAUI ListView列表项的鼠标悬停背景色

要修改ListView列表项的鼠标悬停背景色,你需要替换默认的TextCell为ViewCell(TextCell的样式定制能力有限),并通过VisualStateManager定义悬停状态的样式。以下是修改后的完整代码:

<ListView x:Name="lvWords" ZIndex="4" Grid.Column="0" Grid.Row="1" 
          ItemsSource="{Binding Words}" Margin="10,0,0,0" 
          SeparatorColor="Red" BackgroundColor="Transparent" RowHeight="25">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <VisualStateManager.VisualStateGroups>
                    <VisualStateGroup x:Name="CommonStates">
                        <!-- 鼠标悬停状态 -->
                        <VisualState x:Name="PointerOver">
                            <VisualState.Setters>
                                <Setter Property="BackgroundColor" Value="#333333" />
                            </VisualState.Setters>
                        </VisualState>
                        <!-- 默认状态 -->
                        <VisualState x:Name="Normal">
                            <VisualState.Setters>
                                <Setter Property="BackgroundColor" Value="Transparent" />
                            </VisualState.Setters>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateManager.VisualStateGroups>
                <Label Text="{Binding Word}" TextColor="White" 
                       VerticalOptions="Center" Margin="5,0"/>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

关键说明:

  • 改用ViewCell作为列表项容器,它支持通过VisualStateManager定义不同交互状态的样式。
  • PointerOver状态对应鼠标悬停时的样式,你可以把Value改成任意你需要的背景色值。
  • 保留了你原来的文本颜色、背景透明等设置,确保样式和原有需求一致。
  • 如果需要同时修改选中状态的样式,可以在CommonStates里添加Selected状态的Setter。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 07:31:06