WP8:更改所选内容时更改Listboxitem数据模板 [英] WP8: Change the Listboxitem Datatemplate on Selection Changed

查看:67
本文介绍了WP8:更改所选内容时更改Listboxitem数据模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows Phone 8中有一个列表框,

I have a listbox in windows phone 8,

为此,我要更改选定项目"的数据模板.

For that i want to change the datatemplate for the Selected Item.

我这样做是像WPF一样:

I have done this is WPF like :

<Window.Resources>

        <DataTemplate x:Key="ItemTemplate">
            <TextBlock Text="{Binding}" Foreground="Red" />
        </DataTemplate>

        <DataTemplate x:Key="SelectedTemplate">
            <TextBlock Text="{Binding}" Foreground="Green" />
        </DataTemplate>

        <Style TargetType="{x:Type ListBoxItem}" x:Key="ContainerStyle">
            <Setter Property="ContentTemplate" Value="{StaticResource ItemTemplate}" />
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="ContentTemplate" Value="{StaticResource SelectedTemplate}" />
                </Trigger>
            </Style.Triggers>
        </Style>

    </Window.Resources>
    <ListBox x:Name="lstItems" ItemContainerStyle="{StaticResource ContainerStyle}" />

但是我不知道如何在Windows Phone 8中实现此目标,因为它不

But i don't know how to achieve this in Windows Phone 8 because it doesn't

支持Trigggers

推荐答案

在C#中,您可以轻松使用

in C# you can easily use

ListBox.ItemTemplate =(DataTemplate)this.Resources ["yourkeytemplate"];

ListBox.ItemTemplate = (DataTemplate)this.Resources["yourkeytemplate"];

别忘了在资源XAML中声明您的模板

dont forget to declare your template in resource XAML

这篇关于WP8:更改所选内容时更改Listboxitem数据模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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