wp7 开发学习之 ListBox 应用一

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值