如何更改选定的列表视图项目颜色

Posted

技术标签:

【中文标题】如何更改选定的列表视图项目颜色【英文标题】:How to change selected listview item color 【发布时间】:2017-03-13 06:37:51 【问题描述】:

我有这个列表视图:

<Page.Resources>

<DataTemplate x:Key="ListItemTemplate">
    <TextBlock 
        Text="Binding Name" 
        Style="ThemeResource ListViewItemStyle"
        />

 <ListView 
x:Name="myListView" 
ItemsSource="Binding" 
ItemTemplate="StaticResource ListItemTemplate" 
>

如何改变选中的listviewitem颜色?

【问题讨论】:

【参考方案1】:

为了改变ListView中ListViewItem的Selected颜色,我们需要编辑它的样式如下:

右键单击 ListView 控件->编辑其他模板->编辑生成的项目容器(ItemContainerStyle)->编辑副本: 然后你会得到下面的xaml,请把ListViewItemPresenter里面的这个SelectedBackground属性修改成你喜欢的颜色如下:

    <DataTemplate x:Key="ListItemTemplate">
        <TextBlock Text="Binding name" />
    </DataTemplate>
    <Style x:Key="ListViewItemStyle1" TargetType="ListViewItem">
        <Setter Property="FontFamily" Value="ThemeResource ContentControlThemeFontFamily"/>
        <Setter Property="FontSize" Value="ThemeResource ControlContentThemeFontSize"/>
        <Setter Property="Background" Value="ThemeResource ListViewItemBackground"/>
        <Setter Property="Foreground" Value="ThemeResource ListViewItemForeground"/>
        <Setter Property="TabNavigation" Value="Local"/>
        <Setter Property="IsHoldingEnabled" Value="True"/>
        <Setter Property="Padding" Value="12,0,12,0"/>
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="MinWidth" Value="ThemeResource ListViewItemMinWidth"/>
        <Setter Property="MinHeight" Value="ThemeResource ListViewItemMinHeight"/>
        <Setter Property="AllowDrop" Value="False"/>
        <Setter Property="UseSystemFocusVisuals" Value="True"/>
        <Setter Property="FocusVisualMargin" Value="0"/>
        <Setter Property="FocusVisualPrimaryBrush" Value="ThemeResource ListViewItemFocusVisualPrimaryBrush"/>
        <Setter Property="FocusVisualPrimaryThickness" Value="2"/>
        <Setter Property="FocusVisualSecondaryBrush" Value="ThemeResource ListViewItemFocusVisualSecondaryBrush"/>
        <Setter Property="FocusVisualSecondaryThickness" Value="1"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListViewItem">
                    <ListViewItemPresenter CheckBrush="ThemeResource ListViewItemCheckBrush" ContentMargin="TemplateBinding Padding" CheckMode="ThemeResource ListViewItemCheckMode" ContentTransitions="TemplateBinding ContentTransitions" CheckBoxBrush="ThemeResource ListViewItemCheckBoxBrush" DragForeground="ThemeResource ListViewItemDragForeground" DragOpacity="ThemeResource ListViewItemDragThemeOpacity" DragBackground="ThemeResource ListViewItemDragBackground" DisabledOpacity="ThemeResource ListViewItemDisabledThemeOpacity" FocusVisualPrimaryBrush="TemplateBinding FocusVisualPrimaryBrush" FocusVisualSecondaryThickness="TemplateBinding FocusVisualSecondaryThickness" FocusBorderBrush="ThemeResource ListViewItemFocusBorderBrush" FocusVisualMargin="TemplateBinding FocusVisualMargin" FocusVisualPrimaryThickness="TemplateBinding FocusVisualPrimaryThickness" FocusSecondaryBorderBrush="ThemeResource ListViewItemFocusSecondaryBorderBrush" FocusVisualSecondaryBrush="TemplateBinding FocusVisualSecondaryBrush" HorizontalContentAlignment="TemplateBinding HorizontalContentAlignment" Control.IsTemplateFocusTarget="True" PointerOverForeground="ThemeResource ListViewItemForegroundPointerOver" PressedBackground="ThemeResource ListViewItemBackgroundPressed" PlaceholderBackground="ThemeResource ListViewItemPlaceholderBackground" PointerOverBackground="ThemeResource ListViewItemBackgroundPointerOver" ReorderHintOffset="ThemeResource ListViewItemReorderHintThemeOffset" SelectedPressedBackground="ThemeResource ListViewItemBackgroundSelectedPressed" SelectionCheckMarkVisualEnabled="ThemeResource ListViewItemSelectionCheckMarkVisualEnabled" SelectedForeground="Red" SelectedPointerOverBackground="ThemeResource ListViewItemBackgroundSelectedPointerOver" VerticalContentAlignment="TemplateBinding VerticalContentAlignment"
SelectedBackground="Red"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Page.Resources>
<Grid Background="ThemeResource ApplicationPageBackgroundThemeBrush">
    <ListView x:Name="myListView" ItemTemplate="StaticResource ListItemTemplate" ItemContainerStyle="StaticResource ListViewItemStyle1"/>
</Grid>

【讨论】:

以上是关于如何更改选定的列表视图项目颜色的主要内容,如果未能解决你的问题,请参考以下文章

即使焦点在另一个控件上,如何更改列表视图选定的行背景颜色?

ListView:如何从列表中访问视图以更改背景颜色?

列表视图中非活动选定项目的系统颜色?

如何更改列表视图特定项目的颜色?

当列表视图的项目在颤动的视口中可见时,如何更改项目颜色并执行一些操作?

如何在swift 3的表格视图中仅更改选定行和未选定行的字体颜色和大小?