template selection for WPF

Source: Internet
Author: User
Tags xpath

Typically, the template is associated with a particular piece of data, but it is often desirable to dynamically determine which template to use---can be based on either a property value or a global state. You can also use datatemplateselector when you really need a large replacement template.

Datatemplateselector provides a single method----selecttemplate that allows you to decide which template to use by executing any logic. You can find templates in the included elements, return some hard-coded templates, and even dynamically create templates for each entry.

First, create a class that inherits from Datatemplateselector and complete some of the logic that rotates in several templates. In this example, the XmlElement localname is found and the resource with that name is obtained from the container, as follows:

public class Localnametemplateselector:datatemplateselector
    
   {public
    
       override DataTemplate Selecttemplate ( Object Item,dependencyobject Container)

       {
    
           XmlElement data = Item as XmlElement;

           if (data!= null)
    
           {return
    
               (FrameworkElement) container). FindResource (data. LocalName) as DataTemplate;
    
           }
    
           return null;
    
       }
    
   }

To initialize all the templates, three templates are built: The brown rectangle for the book, The Silver Circle for the CD, and the Blue circle for the DVD. Because the template selector will look for the local name of the XmlElement, you need to set the x:key for each template as follows:

<datatemplate x:key= "book" datatype= "{x:type sx:xmlelement}" > <stackpanel orientation= "Horizontal"
           ; <rectangle margin= "2" width= "height=" fill= "Brown"/> <textblock "Center" Tex t= "{Binding xpath= @Title}" ></TextBlock> </StackPanel> </DataTemplate> &
               Lt;datatemplate x:key= "CD" datatype= "{x:type sx:xmlelement}" > <stackpanel orientation= "Horizontal" > <ellipse margin= "2" width= "height=" fill= "Silver"/> <textblock verticalalignm Ent= "Center" text= "{Binding xpath= @Title}" ></TextBlock> </StackPanel> &LT;/DATATEMPLATE&G
    
       T
               <datatemplate x:key= "DVD" datatype= "{x:type sx:xmlelement}" > <stackpanel orientation= "Horizontal" > <ellipse margin= "2" width= "height=" fill= "Blue"/> <textblock VerticalalIgnment= "Center" text= "{Binding xpath= @Title}" ></TextBlock> </StackPanel> </datatempla Te>

The rest is to associate the template selector with the listbox instead of setting the static template, which is as follows:

<listbox itemssource= "{Binding xpath=/media/*}" >
           <ListBox.ItemTemplateSelector>
               <l: Localnametemplateselector xmlns:l= "Clr-namespace:wpfprogressbardemo"/>
           </listbox.itemtemplateselector >
       </ListBox>

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.