禁用所选Listview项目的蓝色边框

时间:2012-07-02 15:59:03

标签: c# wpf xaml listview

我有一个带有Horizo​​ntal WrapPanel的ListView作为其ItemsPanelTemplate。 我想摆脱所选项目的蓝色背景。它仅在所选项目的左侧可见。

在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 个答案:

答案 0 :(得分:8)

您需要将SystemColors.HighlightBrushKey的{​​{1}}覆盖为ListView(或您想要的任何颜色)

我通常将其放在Transparent中,因此它仅适用于我的应用中的特定ListView.Resources,而不是全部ListView

ListViews

它与您的代码中的内容非常接近,但您需要为<ListView.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/> </ListView.Resources> 设置它,而不是ListView.Resources

答案 1 :(得分: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>

答案 2 :(得分: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>

答案 3 :(得分:0)

要删除所有默认样式(悬停,选择等),只需为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>

Found on MSDN Forum