【发布时间】:2018-10-26 14:08:22
【问题描述】:
如何在 UWP 中完成以下任务?
- 从
ListView中仅选择一项 - 将所选项目的颜色从蓝色更改为红色
- 访问所选项目的所有属性
【问题讨论】:
-
你的问题解决了吗?
如何在 UWP 中完成以下任务?
ListView 中仅选择一项
【问题讨论】:
设置 IsItemClickEnabled="true" 并制作 ItemClick 事件处理程序
在后面的代码中
private void ListView_ItemClick(object sender, ItemClickEventArgs e)
{
// retrieve all properties from e.ClickedItem
// cast it to your model class
var obj = e.ClickedItem;
}
【讨论】:
- 将所选项目的颜色从蓝色更改为红色
Ashiq Hassan 已回答您的第 1 和第 3 个问题。我只是补充回答你的第二个问题。
将所选项目的背景颜色从蓝色更改为红色。您可以直接更改ListViewItem styles and templates。它比在代码隐藏中更改它更简单。只需在 ListViewItem 的 ControlTemplate 中设置ListViewItemPresenter 的SelectedBackground 属性即可。
<Page.Resources>
<x:Double x:Key="ListViewItemContentOffsetX">-40.5</x:Double>
<x:Double x:Key="ListViewItemDisabledThemeOpacity">0.55</x:Double>
<x:Double x:Key="ListViewItemDragThemeOpacity">0.60</x:Double>
<x:Double x:Key="ListViewItemReorderHintThemeOffset">10.0</x:Double>
<x:Double x:Key="ListViewItemSelectedBorderThemeThickness">4</x:Double>
<x:Double x:Key="ListViewItemMinWidth">88</x:Double>
<x:Double x:Key="ListViewItemMinHeight">44</x:Double>
<Thickness x:Key="ListViewItemCompactSelectedBorderThemeThickness">4</Thickness>
<Style TargetType="ListViewItem">
<Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" />
<Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}" />
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundBaseHighBrush}" />
<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="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="Red"
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>
</Setter.Value>
</Setter>
</Style>
</Page.Resources>
<Grid>
<ListView>
<ListViewItem>abc</ListViewItem>
<ListViewItem>def</ListViewItem>
<ListViewItem>ghi</ListViewItem>
</ListView>
</Grid>
【讨论】: