博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

listbox控件的数据绑定-带图片

Posted on 2011-03-09 15:52  星尘的天空  阅读(543)  评论(0编辑  收藏  举报

关于listbox控件带图片列表形式的绑定

系统运行的效果

参考代码如下:

这里一定要注意的是:数据绑定的格式,

一定要注意绑定他们的属性,而不是绑定他们的成员变量

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace WindowsPhoneApplication1.classLibrary
{
    public class Book
    {
        public string bookName = string.Empty;
        public string bookprice = string.Empty;
        public string bookpic = string.Empty;
        //
        public string bPrice
        {
            set
            {
                bookprice = value;
            }
            get
            {
                return bookprice;
            }
        }
        //
        public string bName
        {
            set
            {
                bookName = value;
            }
            get
            {
                return bookName;
            }
        }
        //
        public string bPic
        {
            set
            {
                bookpic = value;
            }
            get
            {
                return bookpic;
            }
        }
        public Book()
        {

        }
        public Book(string name, string price, string pic)
        {
            this.bookName = name;
            this.bookprice = price;
            this.bookpic = pic;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using WindowsPhoneApplication1.classLibrary;

namespace WindowsPhoneApplication1.page
{
    public partial class listboxPage : PhoneApplicationPage
    {
        public listboxPage()
        {
            InitializeComponent();

            List<Book> TT = new List<Book>();
            Book bk = new Book("xingchen", "100", "../Images/1.png");
            TT.Add(bk);
            bk = new Book("xingchen", "100", "../Images/1.png");
            TT.Add(bk);
            lstData.ItemsSource = TT;
        }
    }
}

前台代码

//定义前台数据绑定的格式

       <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <ListBox x:Name="lstData">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Image Margin="8" VerticalAlignment="Top" Source="{Binding  Path=bPic}" Width="100" Height="100" />
                            <StackPanel>
                                <TextBlock Margin="8" Width="250" FontSize="36"  Foreground="White" TextWrapping="Wrap" VerticalAlignment="Top" HorizontalAlignment="Left" Text="{Binding  Path=bName}" />
                                <TextBlock Width="100" Margin="8,0,8,8" VerticalAlignment="Top" HorizontalAlignment="Left" Text="{Binding  Path=bPrice,}"/>
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

        </Grid>
    </Grid>