Windows phone 7 如何在wp7中使用Listbox? 我在XAML PAR中有一个ListBox控件。我的ListBox代码是 <ScrollViewer Grid.Row="2" Name="ScrollGrid" VerticalScrollBarVisibility="Auto" VerticalAlignment="Top" Height="Auto" Width="450" Margin="0,100,0,0" > <ListBox x:Name="TransactionList" Grid.Row="2" HorizontalAlignment="Center" Margin="0,0,0,0" Width="400" Height="Auto"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal" VerticalAlignment="Top" Margin="0,-10,0,0" Height="120" Width="400" MouseLeftButtonUp="StackPanel_MouseLeftButtonUp"> <StackPanel Orientation="Horizontal" VerticalAlignment="Top" Height="80" Width="300" Margin="0,0,20,0"> <TextBlock Height="Auto" Margin="20,0,0,0" VerticalAlignment="Center" Text="vodafone vas" FontSize="30" Foreground="Gray" Name="tbCitynameFavorite" /> </StackPanel> <StackPanel Orientation="Vertical" Height="60" Width="60" Margin="0,0,0,30"> <Image Name="imgfevdlt" Width="50" Height="40" VerticalAlignment="Center" FlowDirection="LeftToRight" Source="/VodafoneAugmentedReality;component/Images/ArrowMoreSmall.png" /> </StackPanel> <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Height="10" Width="350" Margin="-360,60,0,0"> <Image Source="/VodafoneAugmentedReality;component/Images/SaperaterLine.png" Width="350" /> </StackPanel> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </ScrollViewer>

Windows phone 7 如何在wp7中使用Listbox? 我在XAML PAR中有一个ListBox控件。我的ListBox代码是 <ScrollViewer Grid.Row="2" Name="ScrollGrid" VerticalScrollBarVisibility="Auto" VerticalAlignment="Top" Height="Auto" Width="450" Margin="0,100,0,0" > <ListBox x:Name="TransactionList" Grid.Row="2" HorizontalAlignment="Center" Margin="0,0,0,0" Width="400" Height="Auto"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal" VerticalAlignment="Top" Margin="0,-10,0,0" Height="120" Width="400" MouseLeftButtonUp="StackPanel_MouseLeftButtonUp"> <StackPanel Orientation="Horizontal" VerticalAlignment="Top" Height="80" Width="300" Margin="0,0,20,0"> <TextBlock Height="Auto" Margin="20,0,0,0" VerticalAlignment="Center" Text="vodafone vas" FontSize="30" Foreground="Gray" Name="tbCitynameFavorite" /> </StackPanel> <StackPanel Orientation="Vertical" Height="60" Width="60" Margin="0,0,0,30"> <Image Name="imgfevdlt" Width="50" Height="40" VerticalAlignment="Center" FlowDirection="LeftToRight" Source="/VodafoneAugmentedReality;component/Images/ArrowMoreSmall.png" /> </StackPanel> <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Height="10" Width="350" Margin="-360,60,0,0"> <Image Source="/VodafoneAugmentedReality;component/Images/SaperaterLine.png" Width="350" /> </StackPanel> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </ScrollViewer>,windows-phone-7,xaml,listbox,Windows Phone 7,Xaml,Listbox,当我运行应用程序时,它不会显示任何数据 但当我注释掉这两个标记时,它将完美运行 <ListBox.ItemTemplate> <DataTemplate> 这里我只有静态值,所以它可以,但当我有多个值时,它会产生问题 请帮我解决这个问题?您必须设置一个类(比如ListBoxItem),该类包含DataTemplate成员值的所有属性 然后定义ListBoxItem的列表,并将其设置为ListBox的ItemSource 示例

当我运行应用程序时,它不会显示任何数据

但当我注释掉这两个标记时,它将完美运行

 <ListBox.ItemTemplate>
                    <DataTemplate>

这里我只有静态值,所以它可以,但当我有多个值时,它会产生问题
请帮我解决这个问题?

您必须设置一个类(比如ListBoxItem),该类包含DataTemplate成员值的所有属性

然后定义ListBoxItem的列表,并将其设置为ListBox的ItemSource

示例代码

public class ListBoxItem
{
   public string CityNameFavorite { set; get; }
   // Other required properties follows here
}

List<ListBoxItem> listBoxItems = new List<ListBoxItem>();
listBoxItems.Add(new ListBoxItem() { CityNameFavorite = "Vodafone vas" });
//Add as many items you need


TransactionList.ItemsSource = listBoxItems;
公共类ListBoxItem
{
公共字符串CityNameFavorite{set;get;}
//其他必需属性如下所示
}
List listBoxItems=新列表();
添加(新的ListBoxItem(){CityNameFavorite=“Vodafone vas”});
//添加所需的任意多个项目
TransactionList.ItemsSource=listBoxItems;
这只是一个如何实现的概述,根据您的需要即兴创作