WPF:如何更改GridView行的MouseOver /选定背景颜色

我很难找到解决方案,希望能对您有所帮助。

我需要能够在鼠标悬停或选择该行时更改GridView行的背景。

这是我的ListView样式。

我最接近答案的是这篇文章(我的样式中省略了代码,因为它没有任何作用):-

鼠标悬停时更改ListViewItem背景颜色

但是,我仍然无法控制鼠标悬停和选择的颜色。我离上面的帖子越来越近,它暗示这是一个按钮,实际上是在进行突出显示和选择,但是我不知道如何将其结合到我现有的样式中。

这是样式的当前效果。选中第一行,将第三行悬停在鼠标上,其他两行均正常。

鼠标悬停/选择的颜色

顺便说一句,我使用的样式是从Blend中提取的,我对其进行了调整以使用我的应用程序颜色。

我认为我的班级代码在这里不重要,但是如果我错了,请纠正我,我将其发布。


www说
浏览 403回答 1
1回答

慕的地6264312

我认为您需要为添加样式ListViewItems并设置ListView.ItemContainerStyle:<!-- New style --><Style x:Key="StyleListViewItem" TargetType="ListViewItem">&nbsp; <Style.Triggers>&nbsp; &nbsp; <Trigger Property="IsMouseOver" Value="True">&nbsp; &nbsp; &nbsp; <Setter Property="Background" Value="Green"/>&nbsp; &nbsp; </Trigger>&nbsp; &nbsp; <Trigger Property="IsSelected" Value="True">&nbsp; &nbsp; &nbsp; <Setter Property="Background" Value="Blue"/>&nbsp; &nbsp; </Trigger>&nbsp; </Style.Triggers></Style><!-- Existing style --><Style x:Key="StyleListView" TargetType="ListView">&nbsp; <Setter Property="ItemContainerStyle" Value="{StaticResource StyleListViewItem}"/>&nbsp; <Setter Property="SnapsToDevicePixels" Value="true"/>&nbsp; <!-- ... --></Style>
打开App,查看更多内容
随时随地看视频慕课网APP