4

我有一个 ListView,它的 ItemsPanelTemplate 为 Horizo​​ntal 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

4 回答 4

9

您需要覆盖SystemColors.HighlightBrushKeyfor the ListViewto be Transparent(或您想要的任何颜色)

我通常把它放在里面,ListView.Resources所以它只适用于特定的ListView,而不是ListViews我的应用程序中的全部

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

它非常接近您在代码中已有的内容,但您需要将其设置ListView.ResourcesListViewItem.Resources

于 2012-07-02T16:21:58.517 回答
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>
于 2012-07-02T16:06:05.067 回答
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>
于 2012-07-02T16:06:11.410 回答
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>

在 MSDN 论坛上找到

于 2019-09-19T16:03:26.857 回答