ListView 的三种数据绑定方式

1.最原始的绑定方式:

 

代码
 
   
public ObservableCollection < object > ObservableObj;
public MainWindow()
{
InitializeComponent();
ObservableObj
= new ObservableCollection < object > ();
ObservableObj.Add(
new { Name = " 帅波 " , Sex = " " , Age = 20 });
ObservableObj.Add(
new { Name = " 帅波2 " , Sex = " " , Age = 60 });
ObservableObj.Add(
new { Name = " 帅波3 " , Sex = " " , Age = 50 });
ObservableObj.Add(
new { Name = " 帅波4 " , Sex = " " , Age = 40 });
ObservableObj.Add(
new { Name = " 帅波5 " , Sex = " " , Age = 20 });
lv.DataContext
= ObservableObj;
}

 

当然了,用list<object> 也可以,只不过数据源变化后,要调用lv.Items.Refresh();

前台如下:

 

代码
 
   
< ListView Height ="122" HorizontalAlignment ="Left" ItemsSource =" {Binding} " Margin ="33,67,0,0" Name ="lv" VerticalAlignment ="Top" Width ="400" >
< ListView.View >
< GridView >
< GridViewColumn DisplayMemberBinding =" {Binding Path=Name} " Width ="50" Header ="姓名" />
< GridViewColumn DisplayMemberBinding =" {Binding Path=Sex} " Width ="50" Header ="性别" />
< GridViewColumn DisplayMemberBinding =" {Binding Path=Age} " Width ="50" Header ="年龄" >
< GridViewColumn.HeaderTemplate >
< DataTemplate >
< TextBlock Text =" {Binding} " Foreground ="Red" />
</ DataTemplate >
</ GridViewColumn.HeaderTemplate >
</ GridViewColumn >
</ GridView >
</ ListView.View >
</ ListView >

2.用xml绑定

 

代码
 
   
< XmlDataProvider x:Key ="xmlData" XPath ="/Root" >
< x:XData >
< Root xmlns ="" >
< Item Name ="刷波波10" Sex ="女" Age ="11" />
< Item Name ="刷波波11" Sex ="男" Age ="11" />
< Item Name ="刷波波12" Sex ="女" Age ="11" />
< Item Name ="刷波波13" Sex ="女" Age ="11" />
< Item Name ="刷波波14" Sex ="男" Age ="11" />
</ Root >
</ x:XData >
</ XmlDataProvider >

< CollectionViewSource x:Key ="viewSource" Source =" {Binding Source={StaticResource xmlData},XPath=Item} " >
< CollectionViewSource.GroupDescriptions >
< PropertyGroupDescription PropertyName ="@Sex" />
</ CollectionViewSource.GroupDescriptions >
</ CollectionViewSource >

listView的每一列绑定,需要加上@符号,Psath也换成XPath

 

代码
 
   
< ListView Height ="122" HorizontalAlignment ="Left" ItemsSource =" {Binding Source={StaticResource viewSource}} "
Margin
="475,67,0,0" Name ="lv2" VerticalAlignment ="Top" Width ="400" >
< ListView.GroupStyle >
< GroupStyle >
< GroupStyle.ContainerStyle >
< Style TargetType =" {x:Type GroupItem} " >
< Setter Property ="Template" >
< Setter.Value >
< ControlTemplate TargetType =" {x:Type GroupItem} " >
< Expander BorderBrush ="Silver" BorderThickness ="1" >
< Expander.Header >
< DockPanel >
< TextBlock Text =" {Binding Path=Name} " Width ="100" />
< TextBlock Text =" {Binding Path=ItemCount} " />
</ DockPanel >
</ Expander.Header >
< Expander.Content >
< ItemsPresenter />
</ Expander.Content >
</ Expander >
</ ControlTemplate >
</ Setter.Value >
</ Setter >
</ Style >
</ GroupStyle.ContainerStyle >
</ GroupStyle >
</ ListView.GroupStyle >
< ListView.View >
< GridView >
< GridViewColumn DisplayMemberBinding =" {Binding XPath=@Name} " Header ="姓名" Width ="100" />
< GridViewColumn DisplayMemberBinding =" {Binding XPath=@Sex} " Header ="性别" Width ="50" />
< GridViewColumn DisplayMemberBinding =" {Binding XPath=@Age} " Header ="年龄" Width ="50" >
< GridViewColumn.HeaderTemplate >
< DataTemplate >
< TextBlock Foreground ="Red" Text =" {Binding} " />
</ DataTemplate >
</ GridViewColumn.HeaderTemplate >
</ GridViewColumn >
</ GridView >
</ ListView.View >
</ ListView >

 

 

3.采用ObjectDataProvider绑定

cs代码如下:

 

代码
 
   
public class DataProvider
{
public ObservableCollection < object > GetData( int limit)
{
ObservableCollection
< object > ObservableObj = new ObservableCollection < object > ();
int i = 0 ;
while (i <= limit)
{
ObservableObj.Add(
new { Name = " 帅波 " + i.ToString(), Sex = " " , Age = 50 + i });
i
++ ;
}

return ObservableObj;
}
}

 

XAML的代码比较简单

  

代码
 
   
< ObjectDataProvider x:Key = " objData " MethodName = " GetData " ObjectType = " {x:Type local:DataProvider} " >
< ObjectDataProvider.MethodParameters >
< sys:Int32 > 4 </ sys:Int32 >
</ ObjectDataProvider.MethodParameters >
</ ObjectDataProvider >

 

最后listView的绑定如下:

 

代码
 
   
< ListView Height ="122" HorizontalAlignment ="Left" ItemsSource =" {Binding Source={StaticResource objData}} " Margin ="33,231,0,0" Name ="lv3" VerticalAlignment ="Top" Width ="400" >
< ListView.View >
< GridView >
< GridViewColumn DisplayMemberBinding =" {Binding Path=Name} " Header ="姓名" Width ="50" />
< GridViewColumn DisplayMemberBinding =" {Binding Path=Sex} " Header ="性别" Width ="50" />
< GridViewColumn DisplayMemberBinding =" {Binding Path=Age} " Header ="年龄" Width ="50" >
< GridViewColumn.HeaderTemplate >
< DataTemplate >
< TextBlock Foreground ="Red" Text =" {Binding} " />
</ DataTemplate >
</ GridViewColumn.HeaderTemplate >
</ GridViewColumn >
</ GridView >
</ ListView.View >
</ ListView >

 

 

搞定。第二个,参考了网上的一个例子,具体网址忘记了。

codeproject有个经典的例子,http://www.codeproject.com/KB/WPF/ListViewDragDropManager.aspx

 本文demo:http://files.cnblogs.com/xiaokang088/ListViewBindingTest.rar

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值