0

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

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

4

1 回答 1

0

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

然后定义 ListBoxItem 的 List 并将其设置为 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;

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

于 2012-10-30T12:51:37.310 回答