WPF —ListView用ItemsSource绑定对象列表

本文通过一个具体的示例展示了如何使用WPF的ListView进行数据绑定。作者创建了一个Room类来存储房间信息,并通过XAML定义了ListView的展示样式。在代码中初始化了一个Room对象列表并将其设置为ListView的数据源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

虽然wpf 开发有段时间了,但是对于绑定数据这块儿,理解的还是不太深入 。

参考了 http://blog.csdn.net/leftfist/article/details/25333425 的列子,也弄了个简单点的绑定对象列表的试了下。


xaml


 <ListView Canvas.Left="59" Canvas.Top="170" Height="253" Name="listView1" Width="714"  >
<span style="white-space:pre">	</span><ListView.ItemTemplate>
<span style="white-space:pre">		</span><DataTemplate>
<span style="white-space:pre">			</span><Canvas  Width="710" Height="30" Background="{Binding Path=background}" > 
<span style="white-space:pre">				</span><Rectangle Canvas.Left="15" Canvas.Top="7"  Name="rectangle51" Style="{StaticResource RectangleLocked}" >
<span style="white-space:pre">					</span><Rectangle.Fill>
<span style="white-space:pre">						</span><ImageBrush ImageSource="{Binding Path=imgSource}" />
<span style="white-space:pre">					</span></Rectangle.Fill>
<span style="white-space:pre">				</span></Rectangle>


<span style="white-space:pre">				</span><Label Canvas.Left="234" Canvas.Top="2" Content="{Binding Path=roomName}" Name="label7"  Style="{StaticResource LabelItem}" Width="107" />
<span style="white-space:pre">				</span><Label Canvas.Left="357" Canvas.Top="2" Content="{Binding Path=createrName}"  Name="label8"  Style="{StaticResource LabelItem}" Width="107" />
<span style="white-space:pre">				</span><Label Canvas.Left="476" Canvas.Top="1" Content="{Binding Path=roomState}" Name="label9" Style="{StaticResource LabelItem}" Width="72" />
<span style="white-space:pre">				</span><Label Canvas.Left="558" Canvas.Top="2" Content="{Binding Path=howmany}" Name="label10" Style="{StaticResource LabelItem}" Width="72" />
<span style="white-space:pre">			</span></Canvas>
<span style="white-space:pre">		</span></DataTemplate>
<span style="white-space:pre">	</span></ListView.ItemTemplate>
</ListView>

样式就不贴了,随意。



代码:

    public class Room
    {
        public string imgSource { set; get; }

        public string createrName { set; get; }

        public string roomName { set; get; }

        public string roomState { set; get; }

        public string howmany { set; get; }

        public string rowBg { set; get; }

        public ImageBrush background { set; get; }

    }

//初始化对象列表,

List<Room> roomList = new List<Room>();

for (int i = 0; i < 15; i++)
{
	var newitem = new Room()
	{
		//rowBg = "images/row_background.png",
		imgSource = "images/locked.png",
		roomName = "room_" + i,
		createrName = "name_" + i,
		roomState = "sate--" + i,
		howmany = "人数_" + i*10,
		
	};
	ImageBrush b = new ImageBrush();
	b.ImageSource = new BitmapImage(new Uri(new FileInfo("images/row_background.png").FullName, UriKind.Absolute));
	b.Stretch = Stretch.Fill;
	newitem.background = b;

	roomList.Add(newitem);
}

listView1.ItemsSource = roomList;

结果


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值