我试图将背景设置为透明,但是你可以在屏幕快照,当鼠标悬停在ListBoxItem
下面看到它显示了在该项目上一个蓝色的矩形:列表框的ItemTemplate背景透明
我使用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>
注:
超链接样式用于给列表框中的超链接控件提供超链接感觉。
列表框'TeamListView'使用ItemTemplate DataTemplate。 ItemTemplate的样式是ListBoxItem,通过将背景设置为透明onMouseHover,其目的是在悬停时删除没有颜色的蓝色。
我错过了什么?
我不明白为什么你必须改变系统颜色本身, 为什么它不能被一个不同的价值源取代? –
@eran otzap:我没有多少理解,你能详细描述你的问题吗? –
为什么给SystemColors.HighlightBrushKey 一个新的值,而不是ListBoxitem的背景,为什么? 为什么不能直接更改ListBoxItem的Background属性? –