2013-05-10 70 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>