ListView 的三种数据绑定方式

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 [email protected]}" Header="姓名" Width="100" />
<GridViewColumn DisplayMemberBinding="{Binding [email protected]}" Header="性别" Width="50" />
<GridViewColumn DisplayMemberBinding="{Binding [email protected]}" 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 的三种数据绑定方式的主要内容,如果未能解决你的问题,请参考以下文章

数据双向绑定的三种方式

原生JS事件绑定的三种方式

原生JS事件绑定的三种方式

后台将数据传回前台的三种绑定的方式(Model,Map.ModelAndView)

js中的事件绑定的三种方式

[UE4]更新UI的三种方式