Windows 8 practical tips series: 19. Use the gridview in Windows 8 (1)

Source: Internet
Author: User

In this article, we will describe the preliminary use of the gridview to show a variety of information.

First, let's take a look at the important properties of the gridview:

    Selectionmode: Set the selection mode (none, single, multiple)

    Isitemclickenabled: Whether selected click events are allowed

    Itemssource: Data source of the data item

    Itemtemplate: Data item presentation template

    Selecteditem: Content of the selected item

Then let's see how to use the gridview.

1. First set the data source entity class and the data source to provide the source.

Public class datamodel {Public String txttitle {Get; set;} Public String txtcontent {Get; set;} Public String imageurl {Get; Set ;}} public class extends roupmodel {Public String grouptitle {Get; set;} public observablecollection <datamodel> itemcontent {Get; Set ;}} public class viewmodeldata {public viewmodeldata () {sourcedata = getdatagroup ();} private observablecollection <your roupmo Del> _ sourcedata; Public observablecollection <export roupmodel> sourcedata {get {return _ sourcedata;} set {_ sourcedata = value ;}} public observablecollection <export roupmodel> getexport roup () {return New observablecollection <your roupmodel> () {New your roupmodel () {grouptitle = "fantasy novels", itemcontent = new observablecollection <datamodel> () {New datamodel () {txttitle = "Panglong", txtcontent = "the size of the blood eye, the hair, the infinite purple eye, the golden ape The king of the nine snakes who destroyed the Earth, carrying the terror thunder and lightning that destroyed the thunder and lightning ...... This is a vast magic world. ", Imageurl =" http://image.qidian.com/books/1017141/1017141.jpg "}, new datamodel () {txttitle =" ", txtcontent =" Li muran, this is the master of the hundreds of directors, at the time of collecting the last treasure in the world, the treasure in the world is reborn. Thunder power, penalty world ", imageurl =" http://image.qidian.com/books/1.jpg "}, new datamodel () {txttitle =" wizards world ", txtcontent =" Crossing the world to become ordinary little family children, start a powerful journey with biochips with analytical capabilities ..... ", imageurl =" http://image.qidian.com/books/2443169/2443169.jpg "}, new datamodel () {txttitle =" kill God ", txtcontent =" in this crazy world, God has no way back to heaven, let me walk the dead bones of the gods all over the world ...... ", Imageurl =" http://image.qidian.com/books/1911245/1911245.jpg "}}, new synchronized roupmodel () {grouptitle =" online game novels ", itemcontent = new observablecollection <datamodel> () {New datamodel () {txttitle = "full-time master", txtcontent = "Top textbook-level master in the glory of online games. The club has been evicted for various reasons, he left his career circle and sent him to an internet cafe to become a small network administrator, ", imageurl =" http://image.qidian.com/books/1887208/1887208.jpg "}, new datamodel () {txttitle =" the rebirth of thieves in the world ", txtcontent = "Lonely Walker, chasing shadows, This is what thieves do Zange. With the memory of a one hundred and eighty-level big thief, back ten years ago, fate made a joke to the hacker ", imageurl =" http://image.qidian.com/books/1693876/1693876.jpg "}, new datamodel () {txttitle = "", txtcontent = "destiny opens in front of me two straight lines that are irrelevant to each other. One line leads to the burning Palace and city in the flame, and the kingdom is overturned, the earth bears suffering, and the creatures endure suffering in this fire ", imageurl =" http://image.qidian.com/books/1784765/1784765.jpg "}, new datamodel () {txttitle =" ", txtcontent = "the light of wisdom from the silver Yao heaven, the guardian of the power of nature, this is the legend of the King of the arknor druid. ", Imageurl =" http://image.qidian.com/books/2172318/2172318.jpg "}}}};}}

2. Transmit the background data source to the foreground

        public MainPage()        {            this.InitializeComponent();            this.DataContext = new ViewModelData();        }

3. the foreground interface binds the data source on the background page and uses the collectionviesource class to set the data source and itempath.

        <Grid.Resources>            <CollectionViewSource x:Name="itemcollectSource" Source="{Binding Sourcedata}"                                     IsSourceGrouped="true" ItemsPath="ItemContent" />            <DataTemplate x:Key="gvtemplate">                <Grid Width="250" Height="200" Background="#33CCCCCC">                    <Grid.ColumnDefinitions>                        <ColumnDefinition Width="110"></ColumnDefinition>                        <ColumnDefinition></ColumnDefinition>                    </Grid.ColumnDefinitions>                    <Image Grid.Column="0" Margin="5,0,0,0" Source="{Binding ImageUrl}" Stretch="None"></Image>                    <TextBlock Grid.Column="1" Margin="15,15,0,0" Foreground="Black" Text="{Binding txtTitle}"                                    FontWeight="Bold" FontSize="16" TextWrapping="Wrap"/>                    <TextBlock Grid.Column="1" Margin="15,40,0,0" Foreground="Black" Text="{Binding txtContent}"                                    FontWeight="Light" FontSize="14" TextWrapping="Wrap"/>                </Grid>            </DataTemplate>        </Grid.Resources>

4. the foreground settings of the gridview are as follows:

<Gridview name = "gv_item" selectionmode = "single" itemssource = "{binding source = {staticresource itemcollectsource}" itemtemplate = "{staticresource gvtemplate}" selecteditem = "{binding itemcontent, mode = twoway} "isitemclickenabled =" true "margin =" 120,140, "> <gridview. itemspanel> <itemspaneltemplate> <stackpanel orientation = "horizontal"/> </itemspaneltemplate> </gridview. itemspanel> <gridview. groupstyle> <groupstyle. headertemplate> <datatemplate> <grid margin = ","> <button automationproperties. name = "group name" content = "{binding grouptitle}"/> </GRID> </datatemplate> </groupstyle. headertemplate> <groupstyle. panel> <itemspaneltemplate> <variablesizedwrapgrid orientation = "vertical" margin = "0, 0, 50, 0"/> </itemspaneltemplate> </groupstyle. panel> </groupstyle> </gridview. groupstyle> </gridview>

Itemspanel is used to set the orientation of data items in the gridview.

Click win8gridview1.rar to download the source code, as shown in the following figure:

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.