码迷,mamicode.com
首页 > 其他好文 > 详细

ListView 的三种数据绑定方式

时间:2016-02-21 17:13:25      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:

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>

ListView 的三种数据绑定方式

标签:

原文地址:http://www.cnblogs.com/wlming/p/5205080.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!