【发布时间】:2016-02-29 17:07:33
【问题描述】:
有没有办法在选择 ListviewItem 时更改它的属性?
例如,我希望 ListviewItem 内的矩形在选中时为红色,默认为蓝色。
如何以优雅的方式实现这一点?
【问题讨论】:
标签: c# windows win-universal-app
有没有办法在选择 ListviewItem 时更改它的属性?
例如,我希望 ListviewItem 内的矩形在选中时为红色,默认为蓝色。
如何以优雅的方式实现这一点?
【问题讨论】:
标签: c# windows win-universal-app
您可以设置ListView.ItemContainerStyle,自定义ListViewItems在ListView中使用的样式。
此页面显示默认样式:https://msdn.microsoft.com/en-us/library/windows/apps/mt299136.aspx
在您的示例中 - 您可以在类似于以下的代码中更改 Selected~Background 属性:
<ListView ...>
<ListView.ItemContainerStyle>
<Style
TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<ListViewItemPresenter
ContentTransitions="{TemplateBinding ContentTransitions}"
SelectionCheckMarkVisualEnabled="True"
CheckBrush="{ThemeResource SystemControlForegroundBaseMediumHighBrush}"
CheckBoxBrush="{ThemeResource SystemControlForegroundBaseMediumHighBrush}"
DragBackground="{ThemeResource ListViewItemDragBackgroundThemeBrush}"
DragForeground="{ThemeResource ListViewItemDragForegroundThemeBrush}"
FocusBorderBrush="{ThemeResource SystemControlForegroundAltHighBrush}"
FocusSecondaryBorderBrush="{ThemeResource SystemControlForegroundBaseHighBrush}"
PlaceholderBackground="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}"
PointerOverBackground="{ThemeResource SystemControlHighlightListLowBrush}"
PointerOverForeground="{ThemeResource SystemControlHighlightAltBaseHighBrush}"
SelectedBackground="{ThemeResource SystemControlHighlightListAccentLowBrush}"
SelectedForeground="{ThemeResource SystemControlHighlightAltBaseHighBrush}"
SelectedPointerOverBackground="{ThemeResource SystemControlHighlightListAccentMediumBrush}"
PressedBackground="{ThemeResource SystemControlHighlightListMediumBrush}"
SelectedPressedBackground="{ThemeResource SystemControlHighlightListAccentHighBrush}"
DisabledOpacity="{ThemeResource ListViewItemDisabledThemeOpacity}"
DragOpacity="{ThemeResource ListViewItemDragThemeOpacity}"
ReorderHintOffset="{ThemeResource ListViewItemReorderHintThemeOffset}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
ContentMargin="{TemplateBinding Padding}"
CheckMode="Inline"/>
</ControlTemplate>
【讨论】:
对于以后发现此问题的任何人,我已通过 Nuget 中的一个库解决了此问题:https://github.com/JerryNixon/Template10.ListHelpers
它的行为对每个状态使用不同的样式。
<Style x:Key="ItemNormalStyle" TargetType="Grid">
<Setter Property="RequestedTheme" Value="Dark" />
<Setter Property="Background" Value="{ThemeResource ButtonPointerOverBackgroundThemeBrush}" />
</Style>
<Style x:Key="ItemSelectedStyle" TargetType="Grid">
<Setter Property="RequestedTheme" Value="Light" />
<Setter Property="Background" Value="{ThemeResource ButtonBackgroundThemeBrush}" />
</Style>
使用它也非常简单。这是一个附加属性。
<ListView
helpers:ListViewHelper.SelectedItemStyle="{StaticResource MySelectorInfo}"
ItemTemplate="{StaticResource ListViewItemTemplate}" >
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="Padding" Value="0" />
</Style>
</ListView.ItemContainerStyle>
</ListView>
//祝你好运
【讨论】:
我已经在其他地方回答了这个问题,请检查一下! UWP gridview item selection style
【讨论】: