xaml页面
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ListBox
HorizontalAlignment="Left"
Margin="12,6,0,0"
Name="listBox1"
VerticalAlignment="Top"
>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Width="100" Height="100"
Source="{Binding soure}" />
<TextBlock Text="{Binding text}"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
cs 页面
public partial class ListBox_eg : PhoneApplicationPage
{
//一个类用于 保存数据的
public class Item
{
public ImageSource soure { get; set; }
public string text { get; set; }
}
//构造函数
public ListBox_eg()
{
InitializeComponent();
//委托事件 当listbox选中改变的时候触发
listBox1.SelectionChanged += new SelectionChangedEventHandler(listBox1_SelectionChanged);
}
//加载
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
//声明一个集合
List<Item> items = new List<Item>();
//实例化一个类 然后把数据通过类加到集合中的
Item t = new Item();
t.soure = new BitmapImage(new Uri("/Image/1.jpg",UriKind.Relative));
t.text = "1.jpg";
items.Add(t);
t = new Item();
t.soure = new BitmapImage(new Uri("/Image/2.jpg", UriKind.Relative));
t.text = "2.jpg";
items.Add(t);
t = new Item();
t.soure = new BitmapImage(new Uri("/Image/3.jpg", UriKind.Relative));
t.text = "3.jpg";
items.Add(t);
//把集合赋值到listbox集合中
listBox1.ItemsSource = items;
}
//获取选中改变事件
void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//获取当前被选中的对象 然后进行操作
ListBox lb = sender as ListBox;
Item item = lb.SelectedItem as Item;
this.PageTitle.Text = item.text;
}
}