2013-05-10 55 views
24

我正在開發一個具有日誌的WPF應用程序。日誌是用listView設計的。當鼠標結束或選擇項目時,listview項目最不會改變外觀。我已經解決了這種風格爲ListView:WPF:從ListViewItem刪除突出顯示效果

<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> 

這種風格固定我的問題,但提出了一個新的問題。當背景設置爲「透明」時,現在我可以看到鼠標懸停在列表視圖項目上時在下圖中顯示的懸停/光澤效果。

enter image description here

我試圖用這樣的嘗試來解決這個問題,但沒有運氣。

<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> 

任何人都有一個想法如何消除這種惱人的懸停效應?

在此先感謝

回答

72

我不知道這是唯一的解決辦法,但我做了如下(通過設置ListViewItem S的Template屬性):

<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> 
+0

謝謝,這解決了我的問題:)使用在這兩天被! 我想知道如果你知道一本好書或一些好的主頁可以獲得有關造型的知識嗎? – mskydt86 2013-05-11 08:40:31

+0

只有[msdn](http://msdn.microsoft.com/en-US/),http://wpftutorial.net/和Google,對不起..也許別人有一個想法。 – Matmarbon 2013-05-11 09:07:18

+11

FWIW,如果有人遇到這個人,他們也定義了自己的'ListView.ItemTemplate',則需要用''替換'ControlTemplate'標籤中的所有內容。 – 2015-10-07 16:40:13

2

對於我來說,效果很好,像這樣:

<ListView.ItemContainerStyle> 
    <Style TargetType="ListViewItem"> 
    <Style.Triggers> 
     <Trigger Property="IsMouseOver" Value="True"> 
     <Setter Property="Background" Value="Transparent" /> 
     <Setter Property="BorderBrush" Value="Transparent" /> 
     <Setter Property="BorderThickness" Value="0" /> 
     </Trigger> 
    </Style.Triggers> 
    </Style> 
</ListView.ItemContainerStyle> 
2

這項工作:

<Style TargetType="{x:Type ListViewItem}"> 
<Setter Property="Background" Value="Transparent" /> 
<Setter Property="BorderBrush" Value="Transparent"/> 
<Setter Property="VerticalContentAlignment" Value="Center"/> 
<Setter Property="Template"> 
    <Setter.Value> 
     <ControlTemplate TargetType="{x:Type ListViewItem}"> 
      <Grid Background="{TemplateBinding Background}"> 
       <Border Name="Selection" Visibility="Collapsed" /> 
       <!-- This is used when GridView is put inside the ListView --> 
       <GridViewRowPresenter Grid.RowSpan="2" 
             Margin="{TemplateBinding Padding}" 
             HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
             VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
             SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 

      </Grid> 
     </ControlTemplate> 
    </Setter.Value> 
</Setter> 

1

沒有這個問題,但這個工作對我來說。

<Style TargetType="{x:Type ListBoxItem}"> 
      <Setter Property="Background" Value="Transparent" /> 
      <Setter Property="BorderBrush" Value="Transparent"/> 
      <Setter Property="VerticalContentAlignment" Value="Center"/> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
         <Border Name="Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}"> 
           <ContentPresenter Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" Margin="{TemplateBinding Padding}" /> 
          </Border> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
0

我有同樣的問題,但沒有答案幫助我。在網上搜索我發現這個解決方案,它的工作原理:

 <ListView.Resources> 
      <Style TargetType="{x:Type ListViewItem}"> 
       <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate TargetType="{x:Type ListViewItem}"> 
          <GridViewRowPresenter /> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
      </Style> 
     </ListView.Resources> 
0

試試這個它爲我工作。

     <Style TargetType="{x:Type ListBoxItem}"> 
          <Setter Property="Background" Value="Transparent" /> 
          <Setter Property="Template"> 
           <Setter.Value> 
            <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
             <ContentPresenter HorizontalAlignment="Left" Margin="2,3,2,3" /> 
            </ControlTemplate> 
           </Setter.Value> 
          </Setter> 
         </Style> 
        </ListBox.ItemContainerStyle>