如何在wp7中使用Listbox?

时间:2012-10-30 12:28:35

标签: windows-phone-7 xaml listbox

我的xaml par中有一个列表框控件。我的列表框代码是

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

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

但是当我评论出这两个标签时它会完全运行

 <ListBox.ItemTemplate>
                    <DataTemplate>

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

1 个答案:

答案 0 :(得分:0)

您必须设置一个类(比如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;

这只是概述如何实现,根据您的需求即兴创作