WPF更改ListView内容(ContentPresenter)高亮颜色

时间:2018-09-29 10:17:52

标签: wpf xaml styles

当内容表示“ ContentPresenter”时,如何在鼠标悬停时更改列表视图中内容的背景颜色。

 <ListView Visibility="{Binding ListViewVisibility}" Style="{StaticResource MenuListViewStyle}">
        <ContentPresenter Content="{Binding Item, ElementName=menuButton}"/>
    </ListView>

我想将此蓝色背景更改为透明,看起来像这样:Image

1 个答案:

答案 0 :(得分:0)

您可以使用列表视图 ItemContainerStyle 对其进行自定义。

<ListView.ItemContainerStyle>
            <Style TargetType="{x:Type ListViewItem}">
                <Setter Property="Background" Value="Transparent"/>
                <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListViewItem}">
                            <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="Transparent"  Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                                <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsMouseOver" Value="true">
                                    <Setter Property="BorderThickness" Value="1" />
                                    <Setter Property="BorderBrush" Value="#70C0E7" />
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListView.ItemContainerStyle>