2013-09-22 42 views
2

我试图将背景设置为透明,但是你可以在屏幕快照,当鼠标悬停在ListBoxItem下面看到它显示了在该项目上一个蓝色的矩形:列表框的ItemTemplate背景透明

ListViewItem Screen Shot

我使用MVVM,我的实现如下:

<UserControl.Resources> 
    <Style x:Key="HyperLinkStyle" TargetType="{x:Type Hyperlink}"> 
     <Setter Property="Foreground" Value="#FF0066CC"/> 
     <Setter Property="TextDecorations" Value="None" /> 
     <Style.Triggers> 
      <Trigger Property="IsMouseOver" Value="True"> 
       <Setter Property="Foreground" Value="#FF0066CC"/> 
       <Setter Property="TextDecorations" Value="Underline" /> 
      </Trigger> 
      <Trigger Property="IsEnabled" Value="False"> 
       <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
      </Trigger> 
      <Trigger Property="IsEnabled" Value="True"> 
       <Setter Property="Cursor" Value="Hand"/> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
</UserControl.Resources> 

<Grid> 
    <Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="0, 10, 0, 0"> 
     <ListBox x:Name="TeamListView" ItemsSource="{Binding Teams}" BorderThickness="0" 
       SelectionMode="Single" Background="Transparent"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <DataTemplate.Resources> 
         <Style TargetType="ListBoxItem"> 
          <Setter Property="Background" Value="Transparent"/> 
          <Style.Triggers> 
           <Trigger Property="IsMouseOver" Value="True"> 
            <Setter Property="Background" Value="Transparent"/> 
           </Trigger> 
          </Style.Triggers> 
         </Style> 
        </DataTemplate.Resources> 
        <TextBlock Margin="0, 0, 0, 5"> 
          <Hyperlink Style="{Binding Source={StaticResource HyperLinkStyle}}" 
             Command="{Binding ElementName=TeamListView, Path=DataContext.ConnectToTeam}" 
             CommandParameter="{Binding}"> 
           <TextBlock Text="{Binding Path=DisplayName}" /> 
          </Hyperlink> 
        </TextBlock> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
    </Grid> 
</Grid> 

注:

  1. 超链接样式用于给列表框中的超链接控件提供超链接感觉。

  2. 列表框'TeamListView'使用ItemTemplate DataTemplate。 ItemTemplate的样式是ListBoxItem,通过将背景设置为透明onMouseHover,其目的是在悬停时删除没有颜色的蓝色。

我错过了什么?

回答

1

尝试在​​添加此和删除IsMouseOver触发:

<ListBox.Resources> 
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" /> 
</ListBox.Resources> 

根据您的系统主题,系统具有默认高亮笔刷。要更改此值,有必要转到SystemColors。从MSDN

引用:

将SystemColor类提供访问系统画笔和颜色,如ControlBrush,ControlBrushKey,和DesktopBrush。系统画笔是SolidColorBrush对象,该对象用指定的系统颜色绘制区域。系统刷子总是会产生固体填充;它不能用于创建渐变。

您可以使用系统画笔作为静态或动态资源。如果用户在应用程序运行时更改系统画笔,并希望画笔自动更新,请使用动态资源;否则,请使用静态资源。

.NET 4.5系统不使用SystemColors,因此,你应该:

+0

我不明白为什么你必须改变系统颜色本​​身, 为什么它不能被一个不同的价值源取代? –

+0

@eran otzap:我没有多少理解,你能详细描述你的问题吗? –

+0

为什么给SystemColors.HighlightBrushKey 一个新的值,而不是ListBoxitem的背景,为什么? 为什么不能直接更改ListBoxItem的Background属性? –

1

如果你只是想删除你的ListBoxItem你可以设置系统的颜色突出显示做象下面这样:

<Style TargetType="ListBoxItem"> 
    <Style.Resources> 
     <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" color="Transparent" /> 
     <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" /> 
    </Style.Resources> 
+0

为什么说,一旦属性已经被动态资源设置了,不能被覆盖? –

+0

不起作用。在UserControl.Resources中添加此样式定义后,我看不到任何背景颜色的影响,是否需要执行其他任何操作来将此样式与ListBoxItem关联? –

+0

@Tarun Arora:你使用.NET 4.5? –