Three ways to bind data in a ListView

Source: Internet
Author: User

1. The most primitive way to bind:

Public observablecollection<object> observableobj;
Public MainWindow ()
{
InitializeComponent ();
Observableobj = new observablecollection<object> ();
Observableobj.add (New {Name = "Biopo", Sex = "male", age = 20});
Observableobj.add (New {Name = "Biopo 2", Sex = "female", age = 60});
Observableobj.add (New {Name = "Biopo 3", Sex = "female", age = 50});
Observableobj.add (New {Name = "Biopo 4", Sex = "female", age = 40});
Observableobj.add (New {Name = "Biopo 5", Sex = "male", age = 20});
Lv. DataContext = Observableobj;
}

Of course, with list<object> can also, but the data source changes, to call the LV. Items.refresh ();

The front desk is as follows:

<listview height= "122" horizontalalignment= "left" itemssource= "{Binding}" margin= "33,67,0,0" name= "LV" Verticalalignment= "Top" width= ">"
<ListView.View>
<gridview >
<gridviewcolumn displaymemberbinding= "{Binding path=name}" width= "" header= "name"/>
<gridviewcolumn displaymemberbinding= "{Binding path=sex}" width= "" header= "gender"/>
<gridviewcolumn displaymemberbinding= "{Binding path=age}" width= "header=" Age ">
<GridViewColumn.HeaderTemplate>
<DataTemplate>
<textblock text= "{Binding}" foreground= "Red"/>
</DataTemplate>
</GridViewColumn.HeaderTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>

2. Binding with XML

<xmldataprovider x:key= "XmlData" xpath= "/root" >
<x:XData>
<root xmlns= "" >
<item name= "Brush wave ten" sex= "female" age= "one"/>
<item name= "Brush wave One by one" sex= "male" age= "one"/>
<item name= "Brush wave" sex= "female" age= "one"/>
<item name= "Brush wave" sex= "female" age= "one"/>
<item name= "Brush wave" sex= "male" age= "one"/>
</Root>
</x:XData>
</XmlDataProvider>

<collectionviewsource x:key= "Viewsource" source= "{Binding source={staticresource Xmldata},xpath=item}" >
<CollectionViewSource.GroupDescriptions>
<propertygroupdescription propertyname= "@Sex"/>
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>

Each column of the ListView is bound with the @ symbol, and the Psath is also converted to XPath

<listview height= "122" horizontalalignment= "left" itemssource= "{Binding source={staticresource ViewSource}}"
Margin= "475,67,0,0" name= "Lv2" verticalalignment= "Top" width= ">"
<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= "/>"
<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= "name" Width= "/>
<gridviewcolumn displaymemberbinding= "{Binding [email protected]}" header= "Gender" width= "/>"
<gridviewcolumn displaymemberbinding= "{Binding [email protected]}" header= "Age" width= ">
<GridViewColumn.HeaderTemplate>
<DataTemplate>
<textblock foreground= "Red" text= "{Binding}"/>
</DataTemplate>
</GridViewColumn.HeaderTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>

3. Binding with ObjectDataProvider

The CS code is as follows:

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 = "Biopo" + i.tostring (), Sex = "female", age = + i});
i++;
}

return observableobj;
}
}

The code for XAML is relatively simple

<objectdataprovider x:key= "Objdata" methodname= "GetData" objecttype= "{x:type Local:dataprovider}" >
<ObjectDataProvider.MethodParameters>
<sys:Int32>4</sys:Int32>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>

The binding of the last ListView is as follows:

<listview height= "122" horizontalalignment= "left" itemssource= "{Binding source={staticresource ObjData}}" Margin= "33,231,0,0" name= "Lv3" verticalalignment= "Top" width= ">"
<ListView.View>
<GridView>
<gridviewcolumn displaymemberbinding= "{Binding path=name}" header= "name" width= "/>"
<gridviewcolumn displaymemberbinding= "{Binding path=sex}" header= "Gender" width= "/>"
<gridviewcolumn displaymemberbinding= "{Binding path=age}" header= "Age" width= ">"
<GridViewColumn.HeaderTemplate>
<DataTemplate>
<textblock foreground= "Red" text= "{Binding}"/>
</DataTemplate>
</GridViewColumn.HeaderTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>

Three ways to bind data in a ListView

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.