UWP Development: Data Template Selector

Source: Internet
Author: User

These two days, in doing "Jianying UWP" Home page recommendation section, used the data template selector, so a brief introduction. Data template selector. As the name implies, it is possible to selectively display different data templates. So when do I use the data template selector? How to use it? Look down:

Application Scenarios:

In the same way I am currently doing the "Jianying UWP" as an example, in the homepage recommendation section, to show the data are different sections, are placed in a set, so that if the data is directly bound to the ListView or the GridView display, it is not advisable. Although the data is within a collection, the data template selector is needed for this case, because each kind in the collection is different, showing the data differently. Depending on the type, different templates are displayed.

Example Demo:

First step: To use the data template selector, the home page defines a data template selector class. Let this class inherit from: Datatemplateselector. Then define the type of template that needs to be shown. Then rewrite its Selecttemplatecore method. Depending on the type within the collection, different data templates are returned. The code is as follows;

  public sealed class Customdatatemplateselector:datatemplateselector {public DataTemplate collectiontemplate        {get; set;}        Public DataTemplate threadtemplate {get; set;}        Public DataTemplate muscictemplate {get; set;}        Public DataTemplate videotemplate {get; set;}        Public DataTemplate tctemplate {get; set;} protected override DataTemplate Selecttemplatecore (object item, DependencyObject container) {reclist Li            St = Item as reclist;                        if (list = null) {switch (list.type) {case "collection":                    return collectiontemplate;                    Case "Threadmusic": return muscictemplate;                    Case "Threadvideo": return videotemplate;                    Case "Movielines": return tctemplate; Default:return ThreaDtemplate;        }} return null; }    }

Step Two:
Define the required data templates in the foreground, i.e. DataTemplate. Corresponding to the background definition above.
Part of the code is as follows: two different data templates.

Step three: Define the ListView or GridView and then reference the defined data template.

In this way, it is possible to show different data templates based on the objects within the collection. Finally, the previous one, the diagram of the page being made:

This makes it possible to display different types of content within a list. All right, I'll remember it here today. Students who love UWP development join the UWP Development Exchange Group: 193148992. Learn to communicate together.

--it Chase Dream Garden

UWP Development: Data Template Selector

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.