wpf - Creating instances of resources? -


i'm brand spanking new wpf , trying play around projects better understand i'm reading.

my understanding of resource is instance, can't use factory , create instances of it. example, xaml-defined rectangle. can reference it, can't have numerous instances of on surface.

in wpf, way that? if define rectangle resource specific properties , wanted have multiple instances of within dynamically-generated grid, how should going it? or there different way should trying this?

purely academic exercise no real-world application.

actually there's nothing resources in particular prevents using multiple times. perfect example of brush resources, style resources, etc. define them in xaml , xaml parser creates single instance of resources , stores them in resource dictionary , these brushes, styles, etc can used property values many times though single instance of resource created.

but having said that, noted, can't define rectangle resource , use multiple times in visual tree. has nothing fact it's resource, rather has fact frameworkelement cannot child of more 1 parent element.

so have instead called "templates". these tell wpf how create element tree not create tree until instantiate template. below example.

<usercontrol>     <itemscontrol itemssource="{binding wholebunchofitems}">         <itemscontrol.itemtemplate>             <datatemplate>                 <grid>                     <rectangle fill="yellow" />                     <contentpresenter content="{binding}" />                 </grid>             </datatemplate>         </itemscontrol.itemtemplate>     </itemscontrol> </usercontrol> 

in example i've bound itemscontrol collection of sort. each item in collection, itemscontrol use datatemplate render item. within datatemplate can use data binding access current item.

i suggest reading on msdn controltemplate, datatemplate, , style. these important concepts in wpf/silverlight.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -