private void BuildChildren(IEnumerable items, int startingIndex) { IList <View> children = Layout?.Children; if (children == null || items == null) { return; } IDataTemplateSelector templateSel = ItemTemplateSelector; if (templateSel == null && ItemTemplate == null) { templateSel = new DefaultTemplateSelector(this); } if (templateSel != null) { foreach (object item in ItemsSource) { children.Insert(startingIndex++, CreateItemView(item, templateSel.SelectTemplate(item, this))); } } else { DataTemplate template = ItemTemplate; foreach (object item in ItemsSource) { children.Insert(startingIndex++, CreateItemView(item, template)); } } }
/// <summary> /// Return the template to use. This may depend on the Content, or /// other properties. /// </summary> /// <remarks> /// The base class implements the following rules: /// (a) If ContentTemplate is set, use it. /// (b) If ContentTemplateSelector is set, call its /// SelectTemplate method. If the result is not null, use it. /// (c) Look for a DataTemplate whose DataType matches the /// Content among the resources known to the ContentPresenter /// (including application, theme, and system resources). /// If one is found, use it. /// (d) If the type of Content is "common", use a standard template. /// The common types are String, XmlNode, UIElement. /// (e) Otherwise, use a default template that essentially converts /// Content to a string and displays it in a TextBlock. /// Derived classes can override these rules and implement their own. /// </remarks> protected virtual DataTemplate ChooseTemplate() { DataTemplate template = null; object content = Content; // ContentTemplate has first stab template = ContentTemplate; // no ContentTemplate set, try ContentTemplateSelector if (template == null) { if (ContentTemplateSelector != null) { template = ContentTemplateSelector.SelectTemplate(content, this); } } // if that failed, try the default TemplateSelector if (template == null) { template = DefaultTemplateSelector.SelectTemplate(content, this); } return(template); }
public static DefaultTemplateSelector Path(string path) { var selector = new DefaultTemplateSelector(path); return selector; }
public static DefaultTemplateSelector Path(string path) { var selector = new DefaultTemplateSelector(path); return(selector); }