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

相關問題