Dot-Net

從 ListViewItem 中移除高亮效果

  • May 10, 2013

ListView某些情況ListviewItems下,當滑鼠懸停在它們上方或它們被選中時,它們不得改變外觀。

我試圖用這種風格來實現這一點,並取得了一定的成功:

<Style x:Key="ItemContainerStyle1" TargetType="ListViewItem">
   <Setter Property="HorizontalContentAlignment" Value="Left"/>
   <Style.Triggers>
       <Trigger Property="IsMouseOver" Value="True">
           <Setter Property="Background" Value="Transparent" />
           <Setter Property="BorderThickness" Value="0" />
           <Setter Property="Focusable" Value="False" />
       </Trigger>
   </Style.Triggers>
</Style>

但它提出了一個新問題。當背景設置為“透明”時,當滑鼠懸停在列表視圖項上時,我現在可以看到如下圖所示的懸停/光澤效果。

在此處輸入圖像描述

我試圖通過這種嘗試來解決問題,但沒有運氣。

<Style TargetType="{x:Type ListViewItem}">
   <Style.Resources>
     <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#00000000"/>
     <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#00000000"/>
   </Style.Resources>
</Style>

有人知道如何消除這種懸停效果嗎?

我不知道這是否是唯一的解決方案,但我按如下方式進行(通過設置 s 的Template屬性ListViewItem):

<ListView.ItemContainerStyle>
   <Style TargetType="{x:Type ListViewItem}">
       <Setter Property="Background" Value="Transparent" />
       <Setter Property="Template">
           <Setter.Value>
               <ControlTemplate TargetType="{x:Type ListViewItem}">
                   <Border
                        BorderBrush="Transparent"
                        BorderThickness="0"
                        Background="{TemplateBinding Background}">
                       <GridViewRowPresenter HorizontalAlignment="Stretch" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Width="Auto" Margin="0" Content="{TemplateBinding Content}"/>
                   </Border>
               </ControlTemplate>
           </Setter.Value>
       </Setter>
   </Style>
</ListView.ItemContainerStyle>

編輯:

或者正如格蘭特溫尼建議的那樣(我自己沒有測試過):

<ListView.ItemContainerStyle>
   <Style TargetType="{x:Type ListViewItem}">
       <Setter Property="Background" Value="Transparent" />
       <Setter Property="Template">
           <Setter.Value>
               <ControlTemplate TargetType="{x:Type ListViewItem}">
                   <ContentPresenter />
               </ControlTemplate>
           </Setter.Value>
       </Setter>
   </Style>
</ListView.ItemContainerStyle>

引用自:https://stackoverflow.com/questions/16477552