禁用Listview选定项的蓝色边框

4
我有一个ListView,其中ItemsPanelTemplate是水平WrapPanel。 我想要去掉选中项的蓝色背景。只有在选中项左侧才可见。
SO上有许多类似的问题,我尝试了很多解决方案,但都没有起作用。
这是我已经尝试过的:
<ListView.Resources>
            <Style TargetType="{x:Type ListViewItem}">
                <Style.Resources>
                    <!-- Foreground for Selected ListViewItem -->
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" 
                             Color="Black"/>
                    <!-- Background for Selected ListViewItem -->
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
                             Color="Transparent"/>
                    <!--SelectedItem without focus-->
                    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/>
                </Style.Resources>
            </Style>
        </ListView.Resources>



<ListView.ItemContainerStyle>
            <Style TargetType="{x:Type ListViewItem}">
                <EventSetter Event="Control.MouseDoubleClick" Handler="HandleSelectedItemDoubleClick"/>
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="true">
                        <Setter Property="RenderTransform">
                            <Setter.Value>
                                <ScaleTransform ScaleX="2" ScaleY="2" CenterX="12" CenterY="12" />
                            </Setter.Value>
                        </Setter>
                        <Setter Property="Panel.ZIndex" Value="150"/>
                        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
                        <Setter Property="BorderBrush" Value="{x:Null}" />
                        <Setter Property="Background" Value="{x:Null}" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </ListView.ItemContainerStyle>
        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel Orientation="Horizontal" Width="210" Margin="15" />
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>
4个回答

9

您需要覆盖ListViewSystemColors.HighlightBrushKey,使其为Transparent(或您想要的任何颜色)

我通常将此内容放在ListView.Resources中,以便仅适用于特定的ListView,而不是应用程序中的所有ListView

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

这与您的代码非常接近,但需要将其设置为ListView.Resources,而不是ListViewItem.Resources


这个技巧对我没有用,但是禁用FocusVisualStyle却有效。请参考链接获取更多信息。 - Ola Berntsson

2

要移除所有默认样式(悬停、选择等),只需为ItemContainer(而不是Item本身)定义一个自定义模板:

<ListView.ItemContainerStyle>
    <Style TargetType="{x:Type ListViewItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListViewItem}">
                    <ContentPresenter />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ListView.ItemContainerStyle>

在 MSDN 论坛上发现


0

这就是为我所做的:

    <UserControl.Resources>
    <DataTemplate x:Key="ItemTemplate">
        <StackPanel Orientation="Vertical" Background="Transparent" Opacity="1" Width="185" MaxWidth="185">
            <StackPanel Orientation="Horizontal">
            <TextBlock 
                Margin="0,0,10,0"
                            Background="Transparent"
                            Foreground="DarkGoldenrod" 
                            FontSize="12" FontStyle="Italic"
                            Text="{Binding Path=EventTypeName, Mode=OneWay}" />
                <TextBlock
                    Background="Transparent"
                    Foreground="DarkGoldenrod"
                    FontSize="12" FontStyle="Italic"
                    Text="{Binding Path=AccountIdentity, Mode=OneWay}" />
            </StackPanel>
            <TextBlock 
                            Background="Transparent"
                            Foreground="DarkGoldenrod" MaxWidth="170"
                            FontSize="12" FontStyle="Italic" TextTrimming="WordEllipsis" ToolTip="{Binding Path=EventMessage,Mode=OneWay}"
                            Text="{Binding Path=EventMessage, Mode=OneWay}" />

            <TextBlock 
                                Background="Transparent" 
                                Foreground="Black" 
                                FontSize="8" 
                                Text="{Binding Path=EventLoggedOn, Mode=OneWay}"
                                TextTrimming="WordEllipsis"
                                ToolTip="{Binding Path=EventLoggedOn, Mode=OneWay}"
                                Margin="0,0,10,0" />
            <StackPanel Orientation="Horizontal">
                <TextBlock
                                Background="Transparent" 
                                Foreground="Black" 
                                FontSize="8"
                                Text="{Binding Path=QualifiedCreator, Mode=OneWay}" />
            </StackPanel>
        </StackPanel>
    </DataTemplate>
    <DataTemplate x:Key="SelectedTemplate">
        <StackPanel Orientation="Vertical" Background="LightGray" Opacity="1" Width="185" MaxWidth="185">
            <StackPanel Orientation="Horizontal">
                <TextBlock 
                    Margin="0,0,10,0"
                            Background="Transparent"
                            Foreground="Yellow" 
                            FontSize="12" FontStyle="Italic"
                            Text="{Binding Path=EventTypeName, Mode=OneWay}" />
                <TextBlock
                            Background="Transparent"
                    Foreground="Yellow"
                    FontSize="12" FontStyle="Italic"
                    Text="{Binding Path=AccountIdentity, Mode=OneWay}" />
            </StackPanel>
            <TextBlock 
                            Background="Transparent"
                            Foreground="DarkGoldenrod" MaxWidth="170"
                            FontSize="12" FontStyle="Italic" TextTrimming="WordEllipsis" ToolTip="{Binding Path=EventMessage,Mode=OneWay}"
                            Text="{Binding Path=EventMessage, Mode=OneWay}" />

            <TextBlock 
                                Background="Transparent" 
                                Foreground="Black" 
                                FontSize="8" 
                                Text="{Binding Path=EventLoggedOn, Mode=OneWay}"
                                TextTrimming="WordEllipsis"
                                ToolTip="{Binding Path=EventLoggedOn, Mode=OneWay}"
                                Margin="0,0,10,0" />
            <StackPanel Orientation="Horizontal">
                <TextBlock
                                Background="Transparent" 
                                Foreground="Black" 
                                FontSize="8"
                                Text="{Binding Path=QualifiedCreator, Mode=OneWay}" />
            </StackPanel>
        </StackPanel>
    </DataTemplate>
    <Style TargetType="ListViewItem" x:Key="ContainerStyle">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="ContentTemplate" Value="{StaticResource ResourceKey=ItemTemplate}" />
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True" >
                <Setter Property="ContentTemplate" Value="{StaticResource ResourceKey=SelectedTemplate}" />
            </Trigger>
        </Style.Triggers>
    </Style>
</UserControl.Resources>

0

最简单的方法是使用触发器,在选择该项时将背景设置为{x:Null}。

<ListView>
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
                <Style.Triggers>
                        <Trigger Property="IsSelected"
                                         Value="True">
                                <Setter Property="Background"
                                                Value="{x:Null}" />
                                <Setter Property="BorderBrush"
                                                Value="{x:Null}" />
                        </Trigger>
                </Style.Triggers>
        </Style>
    </ListView.ItemContainerStyle>
    ...
</ListView>

在这种情况下,您是否尝试使用Snoop或Wpf Inspector查看突出显示的项目,并找出到底哪个元素被突出显示了?显然不是ListViewItem,但其他某些东西受到了影响。 - Barracoder

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