简体   繁体   English

如何在wp7中使用列表框?

[英]How to use Listbox in wp7?

I have one listbox control in my xaml par.My code for listbox is 我的xaml参数中有一个列表框控件,我的列表框代码是

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

When i run application this will not display any data 当我运行应用程序时,这将不会显示任何数据

But when i comment out this two tag than it will run perfectly 但是当我注释掉这两个标签时,它将完美运行

 <ListBox.ItemTemplate>
                    <DataTemplate>

Here i have only static value so it will ok but when i have multiple value than it will create problem So plase help me i can solve this problem? 在这里,我只有静态值,这样就可以了,但是当我有多个值时,它会产生问题。请帮我解决这个问题?

You will have to set up a class (say ListBoxItem ) which contains all the properties of your DataTemplate members values 您将必须设置一个类(例如ListBoxItem),其中包含DataTemplate成员值的所有属性。

And then define a List of ListBoxItem 's and set it as the ItemSource of your ListBox 然后定义ListBoxItem的List并将其设置为ListBox的ItemSource

Example code 范例程式码

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;

This is just an overview of how it can be achieved, improvise based on your needs 这只是如何实现的概述,根据您的需要即兴进行

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

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