Windows Phone 8中包含多个ItemTemplates的ListBox
本文关键字:ItemTemplates ListBox 包含多 Phone Windows | 更新日期: 2023-09-27 17:49:56
我有一个ListBox,这是一个对话气泡列表(如消息传递应用程序),每一个都可以来自用户或来自系统。因此,在资源中定义的列表框有两个datatemplate。我如何有选择地将这些应用到列表框?
我尝试了DataTemplateSelector(这是WP7的解决方案,但我找不到WP8中的类!),使用(WP8上不支持DataType),最后是ItemTemplate属性上的IValueConvertor -都无济于事!
怎么做呢?我想一定有一个方法,因为这是一个相当简单的要求?
谢谢
DataTemplateSelector
是根据XAML数据绑定中的Item类型更改ItemTemplates
的推荐方法。DataTemplateSelector
没有内置到WP7,也没有内置到WP8。您必须在网上找到您喜欢的DataTemplateSelector
版本并使用它。或者你可以自己编写,因为它只有5-10行代码。
在windowphonegeek上有一篇关于WP7和wp7nl项目中基于其DataTemplateSelector基类的自定义DataTemplateSelectors的好文章。
<ListBox x:Name="listBox" HorizontalContentAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<local:FoodTemplateSelector Content="{Binding}">
<local:FoodTemplateSelector.Healthy>
<DataTemplate>
<StackPanel Orientation="Horizontal" Background="YellowGreen" Width="400" Margin="10">
<Image Source="{Binding IconUri}" Stretch="None"/>
<TextBlock Text="{Binding Name}" FontSize="40" Foreground="Black" Width="280"/>
<TextBlock Text="healty" />
</StackPanel>
</DataTemplate>
</local:FoodTemplateSelector.Healthy>
<local:FoodTemplateSelector.UnHealthy>
<DataTemplate>
<Border BorderBrush="Red" BorderThickness="2" Width="400" Margin="10">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding IconUri}" Stretch="None"/>
<TextBlock Text="{Binding Name}" FontSize="40" Width="280"/>
<Image Source="Images/attention.png" Stretch="None" Margin="10,0,0,0"/>
</StackPanel>
</Border>
</DataTemplate>
</local:FoodTemplateSelector.UnHealthy>
<local:FoodTemplateSelector.NotDetermined>
<DataTemplate>
<StackPanel Orientation="Horizontal" Background="Gray" Width="400" Margin="10">
<Image Source="{Binding IconUri}" Stretch="None"/>
<TextBlock Text="{Binding Name}" FontSize="40" Width="280"/>
<Image Source="Images/question.png" Stretch="None" Margin="10,0,0,0"/>
</StackPanel>
</DataTemplate>
</local:FoodTemplateSelector.NotDetermined>
</local:FoodTemplateSelector>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
如果你正在寻找一个更快速的&Prism的DataTemplateSelector不需要c#编码,因为它使用了DataTemplateSelector。处理类型映射的资源集合。这两个DataTemplateSelector
类都可以复制出来并在你的应用程序中使用。
1: <UserControl.Resources>
2: <DataTemplate x:Key="SelectorDataTemplate">
3: <prism:DataTemplateSelector Content="{Binding}"
4: HorizontalContentAlignment="Stretch"
5: IsTabStop="False">
6: <prism:DataTemplateSelector.Resources>
7: <DataTemplate x:Key="DataType1">
8: <StackPanel Orientation="Horizontal">
9: <TextBlock Text="{Binding ID}"/>
10: <toolkit:Separator />
11: <TextBlock Text="{Binding Name}" />
12: </StackPanel>
13: </DataTemplate>
14:
15: <DataTemplate x:Key="DataType2">
16: <StackPanel Orientation="Horizontal">
17: <TextBox Text="{Binding Index}" />
18: <toolkit:Separator />
19: <TextBox Text="{Binding Description}" />
20: </StackPanel>
21: </DataTemplate>
22:
23: </prism:DataTemplateSelector.Resources>
24: </prism:DataTemplateSelector>
25: </DataTemplate>
26: </UserControl.Resources>
还有更多的DataTemplateSelectors
在那里,包括:奥德赛电话,UIMessage和其他。