0

我创建从罐头模板SplitPage认为有以下ListView的定义:Metro应用:ListView控件的ItemTemplate DataTemplate中为所选项目

<!-- Vertical scrolling item list --> 
<ListView 
    x:Name="itemListView" 
    AutomationProperties.AutomationId="ItemsListView" 
    AutomationProperties.Name="Items" 
    TabIndex="1" 
    Grid.Row="1" 
    Margin="-10,-10,0,0" 
    Padding="120,0,0,60" 
    ItemsSource="{Binding Source={StaticResource itemsViewSource}}" 
    IsSwipeEnabled="False" 
    SelectionChanged="ItemListView_SelectionChanged" 
    ItemTemplate="{StaticResource Standard130ItemTemplate}"/> 

正如你可以看到它使用Standard130ItemTemplate数据模板从StandardStyles.xaml:

<!-- List-appropriate 130 pixel high item template as seen in the SplitPage --> 
<DataTemplate x:Key="Standard130ItemTemplate"> 
    <Grid Height="110" Margin="6"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="Auto"/> 
      <ColumnDefinition Width="*"/> 
     </Grid.ColumnDefinitions> 
     <Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}" Width="110" Height="110"> 
      <Image Source="{Binding Image}" Stretch="UniformToFill" AutomationProperties.Name="{Binding Title}"/> 
     </Border> 
     <StackPanel Grid.Column="1" VerticalAlignment="Top" Margin="10,0,0,0"> 
      <TextBlock Text="{Binding Title}" Style="{StaticResource TitleTextStyle}" TextWrapping="NoWrap"/> 
      <TextBlock Text="{Binding Subtitle}" Style="{StaticResource CaptionTextStyle}" TextWrapping="NoWrap"/> 
      <TextBlock Text="{Binding Description}" Style="{StaticResource BodyTextStyle}" MaxHeight="60"/> 
     </StackPanel> 
    </Grid> 
</DataTemplate> 

问题是即使在选定的项目和鼠标悬停在具有蓝色突出显示的项目上,所有文本都显示为黑色。我想定义一个新模板Standard130SelectedItemTemplate,其中我将文本设置为白色,并且我希望仅在选择时将此数据模板分配给ListView。如何将此数据模板分配给选定项目?

回答

1

在ListView中编辑ListViewItem style。如果你关注它,你会发现一个标题为x:Name="contentPresenter"的部分。这是实际呈现数据模板的内容。如果你去了这种风格的VisualState s,并注意到有标题为“选择”,“选择”等视觉状态

要创建它,要么右键单击设计器中的ListView并转到'编辑附加模板,在你的页面的Resources添加具有TargetType=ListViewItem一个Style,并设置ListView"{StaticResource *InsertStyleKey*}"ItemContainerStyle,或者干脆去你ListView,并在其中添加XAML为<ListView.ItemContainerStyle>

如果您执行涉及创建自己样式的任一项,请将链接的页面中的代码复制到其中,以便可以编辑所有状态。

编辑所选故事板的地方设置ContentPresenter的前景和其更改为白色,像这样:

<VisualState x:Name="Selected"> 
    <Storyboard> 
     <DoubleAnimation Storyboard.TargetName="SelectionBackground" 
      Storyboard.TargetProperty="Opacity" 
      Duration="0" 
      To="1" /> 
     <DoubleAnimation Storyboard.TargetName="SelectedBorder" 
      Storyboard.TargetProperty="Opacity" 
      Duration="0" 
      To="1" /> 
     <DoubleAnimation Storyboard.TargetName="SelectedCheckMark" 
      Storyboard.TargetProperty="Opacity" 
      Duration="0" 
      To="1" /> 
     <!--This is where I have changed the Foreground--> 
     <ObjectAnimationUsingKeyFrames Storyboard.TargetName="contentPresenter" 
      Storyboard.TargetProperty="Foreground"> 
      <DiscreteObjectKeyFrame KeyTime="0" 
       Value="White" /> 
     </ObjectAnimationUsingKeyFrames> 
    </Storyboard> 
</VisualState> 

您可能需要做同样的一些其他国家,使流通权,与一些'PointedOver'状态相同。你现在知道要寻找什么。

+0

嗨,感谢您的建议,但它似乎不起作用。我认为原因在于项目模板的数据模板分配可能会覆盖您在故事板中分配的任何内容。 – 2013-05-11 02:26:32

+0

ItemTemplate实际上被'ItemContainerStyle'包装,也被称为'ListViewItem'样式。如果您查看我提到ContentPresenter的样式,您会注意到它将数据项的值作为Content并将选定的ItemTemplate作为其ContentTemplate。你确定你正确应用了新的Style到ListView吗?施加后 – 2013-05-11 17:48:11

+0

的ListView中定义ItemContainerStyle: 的ItemTemplate = “{StaticResource的Standard130ItemTemplate}” ItemContainerStyle = “{StaticResource的ListViewItemStyle1}” 在ListViewItemStyle1中,ContentPresenter行: 2013-05-14 03:39:24

相关问题