C# ListBox项模板背景透明
我正在尝试将背景设置为透明,但是,正如您在下面的屏幕截图中看到的,当鼠标悬停在C# ListBox项模板背景透明,c#,.net,wpf,listbox,C#,.net,Wpf,Listbox,我正在尝试将背景设置为透明,但是,正如您在下面的屏幕截图中看到的,当鼠标悬停在ListBoxItem上时,它会在项目上方显示一个蓝色矩形: 我正在使用MVVM,我的实现如下: <UserControl.Resources> <Style x:Key="HyperLinkStyle" TargetType="{x:Type Hyperlink}"> <Setter Property="Foreground" Value="#FF0066CC"
ListBoxItem
上时,它会在项目上方显示一个蓝色矩形:
我正在使用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>
注:
我错过了什么 如果您只想删除突出显示的
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>
尝试将此添加到
列表框中。参考资料
并删除IsMouseOver
触发器:
<ListBox.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
</ListBox.Resources>
根据您的系统主题,系统中有默认的高亮显示笔刷。要更改此值,必须转到SystemColors
引自:
SystemColor类提供对系统笔刷和颜色的访问,例如ControlBrush、ControlBrushKey和DesktopBrush。系统笔刷是使用指定的系统颜色绘制区域的SolidColorBrush对象。系统笔刷始终生成实心填充;它不能用于创建渐变
可以将系统笔刷用作静态或动态资源。如果希望在用户在应用程序运行时更改系统笔刷时自动更新笔刷,请使用动态资源;否则,请使用静态资源
在.NET 4.5
中,系统不使用SystemColors
,因此,您应该:
- 创建您的
样式/控制模板
- 寻找替代方案,例如:
- 看看这个,它展示了如何消除框架之间的差异
.NET 4.5
,我不明白为什么您必须更改系统颜色本身,为什么不能用其他值源替换它?@eran otzap:我不太明白,你能详细描述一下你的问题吗?为什么给SystemColor.HighlightBrushKey一个新值而不是listboxitem的背景,为什么?为什么不能直接更改ListBoxItem的背景属性?谢谢您,先生!就这样,.NET4.5,你的建议解决了它。非常感谢。我在这里遵循了答案,@eran otzap:请阅读我答案中的引语<代码>系统颜色-这是在颜色中包含静态变量的类。之所以这样做,是因为系统具有默认的高亮显示笔刷,具体取决于您的系统主题。结果表明,系统的颜色值取自此类SystemColors
,而不是您在触发器中设置的颜色。此结构由WPF开发人员设计。