ListBox ItemTemplate 背景透明化

3
我试图将背景设置为透明,但是如下截图所示,当鼠标悬停在ListBoxItem上时,它会在该项上显示一个蓝色矩形:

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. hyperlinkstyle用于为listbox中的超链接控件提供超链接效果。

  2. 列表框'TeamListView'使用ItemTemplate DataTemplate。ItemTemplate的样式是ListBoxItem,通过在鼠标悬停时将背景设置为透明,意图是去除蓝色但不改变颜色。

我错过了什么?

2个回答

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>

为什么一旦属性被动态资源设置,就不能被覆盖? - eran otzap
不起作用。在UserControl.Resources中添加了这个样式定义后,我没有看到对背景颜色的任何影响,我需要做一些额外的事情来将这个样式与ListBoxItem关联吗? - Tarun Arora
@Tarun Arora:你使用的是.NET 4.5吗? - Anatoliy Nikolaev
@Tarun Arora:请阅读我的答案,其中提到了.NET 4.5 - Anatoliy Nikolaev

1
尝试将以下内容添加到ListBox.Resources中,并删除IsMouseOver触发器:
<ListBox.Resources>
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
</ListBox.Resources>

系统会根据您的系统主题设置默认的突出显示刷子。要更改此值,需要转向SystemColors。 来自MSDN的引用:
SystemColors类提供对系统画笔和颜色的访问,例如ControlBrush、ControlBrushKey和DesktopBrush。系统画笔是一个SolidColorBrush对象,用指定的系统颜色绘制区域。系统画笔总是产生实心填充;它不能用于创建渐变。
您可以将系统画笔用作静态或动态资源。如果希望在应用程序运行时用户更改系统画笔,则使用动态资源自动更新画笔;否则,请使用静态资源。
.NET 4.5中,系统不使用SystemColors,因此,您应该:

我不明白为什么你必须改变系统颜色本身,为什么不能用不同的值来源来替换它? - eran otzap
@eran otzap:我有点不太明白,你能详细描述一下你的问题吗? - Anatoliy Nikolaev
为什么要给SystemColors.HighlightBrushKey赋新值,而不是ListBoxItem的Background?这是为什么呢? 为什么不能直接更改ListBoxItem的Background属性? - eran otzap
1
谢谢您先生!就是这样,.NET 4.5,您的建议解决了它。谢谢。我在这里遵循了答案,https://dev59.com/umct5IYBdhLWcg3wjuAj - Tarun Arora
@eran otzap:请阅读我回答中的引用。SystemColors - 这是包含颜色静态变量的类。这样做是因为系统根据您的系统主题具有默认的突出显示画笔。事实证明,系统颜色的值取自此类SystemColors,而不是从触发器中设置的颜色。这种构造是由WPF开发人员设计的。 - Anatoliy Nikolaev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接