简体   繁体   English

Windows Phone 8中包含多个ItemTemplates的ListBox

[英]ListBox with multiple ItemTemplates in Windows Phone 8

I have a ListBox which is a list of conversation bubbles (like the messaging app), each of which can either be from the User or from the System. 我有一个ListBox,它是一个会话气泡列表(如消息传递应用程序),每个气泡可以来自用户或来自系统。 There are thus two DataTemplates for the listbox which are defined in resources. 因此,列表框中有两个DataTemplates,它们在资源中定义。 How do I selectively apply these to the ListBox? 如何有选择地将这些应用于ListBox?

I tried DataTemplateSelector (this is the solution for WP7, but I can't find the class in WP8!), using (DataType isn't supported on WP8), and finally an IValueConvertor on the ItemTemplate property - all to no avail! 我尝试过DataTemplateSelector(这是WP7的解决方案,但我找不到WP8中的类!),使用(WP8不支持DataType),最后是ItemTemplate属性的IValueConvertor - 一切都无济于事!

What is the way to do this? 这样做的方法是什么? I imagine there must be a way as it's a fairly simple requirement?! 我想必须有一种方法,因为这是一个相当简单的要求?!

Thanks 谢谢

DataTemplateSelector is the recommended way to change ItemTemplates based on Item types in XAML databinding. DataTemplateSelector是基于XAML数据绑定中的Item类型更改ItemTemplates的推荐方法。 DataTemplateSelector wasn't built-in to WP7 and isn't built-in for WP8. DataTemplateSelector不是内置于WP7,也不是WP8内置的。 You'll have to find a version online you like of DataTemplateSelector and use that. 你必须在网上找到一个你喜欢DataTemplateSelector的版本并使用它。 Or just roll your own since it's about 5-10 lines of code. 或者只是滚动自己,因为它是大约5-10行代码。

There's a good article on WindowsPhoneGeek on custom DataTemplateSelectors in WP7 and wp7nl project based its DataTemplateSelector base class on that article. 关于WP7中的自定义DataTemplateSelectors和基于该文章的DataTemplateSelector基类的 wp7nl项目,WindowsPhoneGeek上有一篇很好的文章。

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

If you're looking for a more quick & dirty solution Prism's DataTemplateSelector doesn't require C# coding as it uses the DataTemplateSelector.Resources collection to handle type mapping . 如果您正在寻找更快速和更脏的解决方案, Prism的DataTemplateSelector不需要C#编码,因为它使用DataTemplateSelector.Resources集合来处理类型映射 Both DataTemplateSelector classes can just be copied out and used in your app. 两个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>

There are lots more DataTemplateSelectors out there, including: Odyssey Phone , UIMessage and others. 还有更多的DataTemplateSelectors ,包括: Odyssey PhoneUIMessage等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM