地铁应用:ListView控件的ItemTemplate DataTemplate中为所选项目 [英] Metro app: ListView ItemTemplate DataTemplate for selected item

查看:143
本文介绍了地铁应用:ListView控件的ItemTemplate DataTemplate中为所选项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建从罐头模板SplitPage认为有以下ListView的定义:

I created a SplitPage view from canned templates that has the following ListView definition:

    <!-- 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:

As you can see it uses Standard130ItemTemplate data template from 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。我怎么这个数据模板分配到选定的项目吗?

Problem is all the text appears in black even in selected item and mouse over item which has blue highlight. I would like to define a new template Standard130SelectedItemTemplate where I make the text white and I want to assign this data template to the ListView only when selected. How do I assign this data template to Selected item?

推荐答案

编辑的在ListView ListViewItem的风格。如果你遵循它,你会发现一个标题为部分 X:NAME =内容presenter。这是实际上是presenting您的数据模板。如果你去到的VisualState s这个风格,并注意到有标题为选择可视状态,选择等。

Edit the ListViewItem style in the ListView. If you follow it down, you'll find a section titled x:Name="contentPresenter". This is what's actually presenting your data template. If you go up to the VisualStates of this style and notice that there are visual states titled "Selected", "Selecting", etc.

要创建它,就在设计师的ListView控件可以右键点击并进入编辑其他模板中,添加风格的TargetType = ListViewItem的在页面和您的资源设置<$ C $的 ItemContainerStyle C>的ListView {StaticResource的* InsertStyleKey *},或者干脆转到的ListView ,并在其中添加XAML为&LT; ListView.ItemContainerStyle方式&gt;

To create it, either right click on the ListView in the designer and go to 'Edit Additional Templates', add a Style with TargetType=ListViewItem in your Resources of the page and set the ItemContainerStyle of the ListView to "{StaticResource *InsertStyleKey*}", or simply go to your ListView and add the xaml in it as <ListView.ItemContainerStyle>.

如果你做任何涉及创建您自己的风格的人,从链接到它的页面复制code,让你拥有所有的州提供给编辑。

If you do either of the ones involving creating your own style, copy the code from the page linked into it, so you have all of the states available to edit.

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

Edit the Selected Storyboard where it sets the Foreground of the ContentPresenter and change it to White, like so:

<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状态。你现在知道要寻找什么,虽然。

You may have to do the same to some of the other states to make the flow right, same with some of the 'PointedOver' states. You now know what to look for though.

这篇关于地铁应用:ListView控件的ItemTemplate DataTemplate中为所选项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆