Listbox Binding ItemsSource

时间:2022-05-07 13:11:48

把List<CourseItem>绑定到ListBox.

前台绑定:

                <ListBox x:Name="ItemBox" Grid.Row="1">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Width="400" Height="150" Margin="12,0,0,20">
<Border Background="#01BCF3">
<Image Source="" Stretch="UniformToFill" AutomationProperties.Name="{Binding Title}" />
</Border>
<StackPanel VerticalAlignment="Bottom" Margin="10,0" >
<TextBlock Text="{Binding Title}" TextWrapping="Wrap" Margin="10,0" FontSize="24" Tap="TextBlock_Tap_1" Style="{StaticResource PhoneTextTitle3Style}" />
<TextBlock Text="{Binding PublishDate}" Margin="10,0" FontSize="18" Foreground="#FF8F8F8F"/>
</StackPanel>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

后台绑定:

ItemBox.ItemsSource = CommonConfig.PopularCourses;

其中, PopularCourses 的类型是List<CourseItem>, CourseItem 的定义如下:

        public static List<CourseItem> PopularCourses
{
get;
set;
} class CourseItem
{
private string _title;
public string Title { get { return _title; } set { _title = value; } } private string _id;
public string ID { get { return _id; } set { _id = value; } } private string _description;
public string Description { get { return _description; } set { _description = value; } } private string _totalPoints;
public string TotalPoints { get { return _totalPoints; } set { _totalPoints = value; } } private string _level;
public string Level { get { return _level; } set { _level = value; } } private string _owner;
public string Owner { get { return _owner; } set { _owner = value; } } private string _rating;
public string Rating { get { return _rating; } set { _rating = value; } } private Category _category;
public Category Category { get { return _category; } set { _category = value; } } private DateTime _pubDate;
public DateTime PublishDate { get { return _pubDate; } set { _pubDate = value; } } public List<Module> Modules { get; set; }
}

注意: CourseItem中各属性定义是一定要有get;选择器, 否则绑定后内容不显示。