Disable blue border for selected Listview item

后端 未结 4 941
执念已碎
执念已碎 2020-12-19 23:37

I have a ListView with Horizontal WrapPanel as its ItemsPanelTemplate. I want to get rid of the blue background for selected item. It is visible only on the left of the sele

相关标签:
4条回答
  • 2020-12-19 23:53

    This is what did it for me:

        <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 讨论(0)
  • 2020-12-20 00:02

    To remove all default styling (hovering, selecting, etc.) just define a custom Template for the ItemContainer (not the Item itself):

    <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

    0 讨论(0)
  • 2020-12-20 00:15

    You need to overwrite the SystemColors.HighlightBrushKey for the ListView to be Transparent (or whatever color you want)

    I typically put this in the ListView.Resources so it only applies to the specific ListView, and not all ListViews in my application

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

    Its very close to what you have in your code already, but you need to set it for the ListView.Resources, not ListViewItem.Resources

    0 讨论(0)
  • 2020-12-20 00:15

    The simplest way to do this is to set the background to {x:Null} when the item is selected, using a trigger.

    <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>
    
    0 讨论(0)
提交回复
热议问题