这是XAML:
<ListView Grid.Column="1" Grid.Row="1" Name="TaskListView" HorizontalContentAlignment="Center">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<Grid Margin="0,10,0,0" Background="#00FF0000" AllowFocusOnInteraction="False">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal">
<VisualState.Setters>
<Setter Value="#2c567b" Target="InnerBorder.BorderBrush"/>
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Selected">
<VisualState.Setters>
<Setter Value="White" Target="InnerBorder.BorderBrush"/>
<Setter Value="White" Target="InnerBorder.Background"/>
<Setter Value="#db4662" Target="InnerTextBlock.Foreground"/>
<Setter Value="#db4662" Target="InnerRectangle.Fill"/>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border BorderThickness="4" BorderBrush="#2c567b" Grid.ColumnSpan="2" Name="InnerBorder"></Border>
<Grid Padding="20,20,0,20" Name="InnerG">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Rectangle Fill="#30fff8" Margin="10" Name="InnerRectangle"></Rectangle>
<TextBlock Grid.Column="1" Foreground="#30fff8" Margin="20,0,0,0" Text="{Binding ProductName}" VerticalAlignment="Center"></TextBlock>
</Grid>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
当我仅创建listviewitem的控件模板时,它可以工作。但是,在将其添加/编辑到DataTemplate后,它失败了。
很抱歉,我是UWP的初学者,几乎没有找到创建控制模板/将其添加到listviewitem的数据模板的示例。
您能帮我吗?谢谢。
答案 0 :(得分:2)
首先,您可以按照@Martin的回答来创建ListView的ItemContainerStyle,然后更改ListViewItem的ControlTemplate,这样PointerOver
和Normal
VisualState将会是这样的:
<VisualState x:Name="Normal">
<VisualState.Setters>
<Setter Target="Root.(RevealBrush.State)" Value="PointerOver"/>
<Setter Target="Root.Foreground" Value="#30fff8"/>
<Setter Target="Root.RevealBorderBrush" Value="{ThemeResource ListViewItemRevealBorderBrushPointerOver}"/>
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Selected"/>
<VisualState x:Name="PointerOver">
<VisualState.Setters>
<Setter Target="Root.(RevealBrush.State)" Value="PointerOver"/>
<Setter Target="Root.Foreground" Value="Red"/>
<Setter Target="Root.RevealBorderBrush" Value="{ThemeResource ListViewItemRevealBorderBrushPointerOver}"/>
</VisualState.Setters>
</VisualState>
我们在Root.Foreground
和PointerOver
的VisualState中更改Normal
。 请删除您的DataTemplate中的TextBlock的Foreground="#30fff8"
代码,作为@David的注释。
另一方面,您还可以从C:\ Program Files(x86)\ Windows Kits \ 10 \ DesignTime \ CommonConfiguration \ Neutral \ UAP \ {SDK version} \ Generic \ generic.xaml获取默认的ListViewItem样式。文件。您可以将x:Key =“ ListViewItemExpanded”的样式复制到您的Page.Resource中,并删除x:Key =“ ListViewItemExpanded”,然后此样式将应用于页面中的所有ListViewItems。然后,您可以找到Normal
和PointerOver
可视状态,以根据需要进行一些更改。另请注意,要在数据模板中删除TextBlock的Foreground="#30fff8"
代码。
---更新---
在这里,我将第一种情况下的所有xaml代码粘贴到这里,您可以尝试一下。
<Page.Resources>
<Style x:Key="ListViewItemRevealStyle" 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="{StaticResource UseSystemFocusVisuals}"/>
<Setter Property="FocusVisualMargin" Value="0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<ListViewItemPresenter x:Name="Root" CheckBrush="{ThemeResource ListViewItemCheckBrush}" ContentMargin="{TemplateBinding Padding}" CheckBoxBrush="{ThemeResource ListViewItemCheckBoxBrush}" ContentTransitions="{TemplateBinding ContentTransitions}" CheckMode="{ThemeResource ListViewItemCheckMode}" DragOpacity="{ThemeResource ListViewItemDragThemeOpacity}" DisabledOpacity="{ThemeResource ListViewItemDisabledThemeOpacity}" DragBackground="{ThemeResource ListViewItemDragBackground}" DragForeground="{ThemeResource ListViewItemDragForeground}" FocusBorderBrush="{ThemeResource ListViewItemFocusBorderBrush}" FocusVisualMargin="{TemplateBinding FocusVisualMargin}" FocusSecondaryBorderBrush="{ThemeResource ListViewItemFocusSecondaryBorderBrush}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Control.IsTemplateFocusTarget="True" PressedBackground="{ThemeResource ListViewItemBackgroundPressed}" PlaceholderBackground="{ThemeResource ListViewItemPlaceholderBackground}" PointerOverForeground="{ThemeResource ListViewItemForegroundPointerOver}" PointerOverBackground="{ThemeResource ListViewItemBackgroundPointerOver}" ReorderHintOffset="{ThemeResource ListViewItemReorderHintThemeOffset}" SelectedForeground="{ThemeResource ListViewItemForegroundSelected}" SelectionCheckMarkVisualEnabled="{ThemeResource ListViewItemSelectionCheckMarkVisualEnabled}" SelectedBackground="{ThemeResource ListViewItemBackgroundSelected}" SelectedPressedBackground="{ThemeResource ListViewItemBackgroundSelectedPressed}" SelectedPointerOverBackground="{ThemeResource ListViewItemBackgroundSelectedPointerOver}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal">
<VisualState.Setters>
<Setter Target="Root.(RevealBrush.State)" Value="PointerOver"/>
<Setter Target="Root.Foreground" Value="#30fff8"/>
<Setter Target="Root.RevealBorderBrush" Value="{ThemeResource ListViewItemRevealBorderBrushPointerOver}"/>
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Selected"/>
<VisualState x:Name="PointerOver">
<VisualState.Setters>
<Setter Target="Root.(RevealBrush.State)" Value="PointerOver"/>
<Setter Target="Root.Foreground" Value="Red"/>
<Setter Target="Root.RevealBorderBrush" Value="{ThemeResource ListViewItemRevealBorderBrushPointerOver}"/>
</VisualState.Setters>
</VisualState>
<VisualState x:Name="PointerOverSelected">
<VisualState.Setters>
<Setter Target="Root.(RevealBrush.State)" Value="PointerOver"/>
<Setter Target="Root.RevealBorderBrush" Value="{ThemeResource ListViewItemRevealBorderBrushPointerOver}"/>
</VisualState.Setters>
</VisualState>
<VisualState x:Name="PointerOverPressed">
<VisualState.Setters>
<Setter Target="Root.(RevealBrush.State)" Value="Pressed"/>
<Setter Target="Root.RevealBorderBrush" Value="{ThemeResource ListViewItemRevealBorderBrushPressed}"/>
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Pressed">
<VisualState.Setters>
<Setter Target="Root.(RevealBrush.State)" Value="Pressed"/>
<Setter Target="Root.RevealBorderBrush" Value="{ThemeResource ListViewItemRevealBorderBrushPressed}"/>
</VisualState.Setters>
</VisualState>
<VisualState x:Name="PressedSelected">
<VisualState.Setters>
<Setter Target="Root.(RevealBrush.State)" Value="Pressed"/>
<Setter Target="Root.RevealBorderBrush" Value="{ThemeResource ListViewItemRevealBorderBrushPressed}"/>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="DisabledStates">
<VisualState x:Name="Enabled"/>
<VisualState x:Name="Disabled">
<VisualState.Setters>
<Setter Target="Root.RevealBorderThickness" Value="0"/>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</ListViewItemPresenter>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="ListViewItemContainerStyle1" BasedOn="{StaticResource ListViewItemRevealStyle}" TargetType="ListViewItem"/>
</Page.Resources>
<Grid>
<ListView ItemContainerStyle="{StaticResource ListViewItemContainerStyle1}" Name="TaskListView" HorizontalContentAlignment="Center">
<ListView.ItemTemplate>
<DataTemplate>
<Grid Margin="0,10,0,0" AllowFocusOnInteraction="False" >
<Border BorderThickness="4" BorderBrush="#2c567b" Grid.ColumnSpan="2" Name="InnerBorder"></Border>
<Grid Padding="20,20,0,20" Name="InnerG">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Rectangle Fill="#30fff8" Margin="10" Name="InnerRectangle"></Rectangle>
<TextBlock Grid.Column="1" Margin="20,0,0,0" Text="{Binding ProductName}" VerticalAlignment="Center"></TextBlock>
<!-- You can add other controls and bind data-->
</Grid>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
答案 1 :(得分:1)
原因是,当您像这样定义VisualStates
时,它们属于内部Grid
元素,实际上没有定义任何Selected
状态。当您检查documentation时,可以看到Selected
控件上定义了ListViewItem
状态,该控件充当实际“项目”(从数据模板生成的内容)的包装。因此,您应该为ListViewItem
创建一个自定义样式,并将其分配给ListView.ItemContainerStyle
。
您可以通过多种方式修改样式,但是最简单的方法是将ListView
放在页面上,在 Designer 或 Document Outline 中右键单击它。 strong>,然后选择编辑其他模板,编辑生成的项目容器,最后选择编辑副本... 。这将生成默认ListViewItemStyle
的副本,您可以根据自己的喜好自由对其进行修改。
答案 2 :(得分:0)
@Martin已经解释了解决方案,但是正如您所提到的,您是一个初学者,我想指出一些可能对您有所帮助的事情。
请参阅this MSDN文档,该文档具有为ListView
定义的样式和模板。
在ListView
样式中,您会看到以下“选定的视觉状态”,您可以根据需要修改该状态。
<VisualState x:Name="Selected">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="MultiSelectCheck"
Storyboard.TargetProperty="Opacity"
Duration="0:0:0"
To="1"/>
<DoubleAnimation Storyboard.TargetName="BorderBackground"
Storyboard.TargetProperty="Opacity"
Duration="0"
To="1"/>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="BorderBackground" Storyboard.TargetProperty="Fill">
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightListAccentLowBrush}" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}" />
</ObjectAnimationUsingKeyFrames>
<PointerUpThemeAnimation Storyboard.TargetName="ContentPresenter" />
</Storyboard>
</VisualState>