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