如何更改选定的列表视图项目颜色
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>
【讨论】:
以上是关于如何更改选定的列表视图项目颜色的主要内容,如果未能解决你的问题,请参考以下文章