Windows Phone 8中包含多个项模板的ListBox

时间:2012-12-28 20:43:19

标签: c# xaml windows-phone-8

我有一个ListBox,它是一个会话气泡列表(如消息传递应用程序),每个气泡可以来自用户或来自系统。因此,列表框中有两个DataTemplates,它们在资源中定义。如何有选择地将这些应用于ListBox?

我尝试了DataTemplateSelector(这是WP7的解决方案,但我在WP8中找不到类!),使用(WP8不支持DataType),最后是ItemTemplate属性的IValueConvertor - 全部为no利用!

这样做的方法是什么?我想必须有一种方法,因为这是一个相当简单的要求?!

由于

1 个答案:

答案 0 :(得分:12)

DataTemplateSelector是基于XAML数据绑定中的Item类型更改ItemTemplates的推荐方法。 WP7没有内置DataTemplateSelector,WP8也没有内置DataTemplateSelector。您必须在<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> 找到您喜欢的在线版本并使用它。或者只是滚动自己,因为它是大约5-10行代码。

关于custom DataTemplateSelectors in WP7上的WindowsPhoneGeek和基于该文章的DataTemplateSelector base class的wp7nl项目,有一篇很好的文章。

DataTemplateSelector

如果您正在寻找更快速的&amp;脏解Prism's DataTemplateSelector不需要C#编码uses the DataTemplateSelector.Resources collection to handle type mapping。两个 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

还有更多{{1}},其中包括:Odyssey PhoneUIMessage等。

相关问题