我是靠谱客的博主 爱笑小兔子,最近开发中收集的这篇文章主要介绍ListView 的三种数据绑定方式ListView 的三种数据绑定方式,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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>

转载于:https://www.cnblogs.com/wlming/p/5205080.html

最后

以上就是爱笑小兔子为你收集整理的ListView 的三种数据绑定方式ListView 的三种数据绑定方式的全部内容,希望文章能够帮你解决ListView 的三种数据绑定方式ListView 的三种数据绑定方式所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(51)

评论列表共有 0 条评论

立即
投稿
返回
顶部