xaml - WPF - Defining DataTemplate inside ResourceDictionary without a key -
i have seen many times wpf code samples in form:
<window.resources> <datatemplate datatype="{x:type sometype}"> <!-- elements defining datatemplate--> </datatemplate> </window.resources>
i understand usage, cant understand why syntax ok: since resourcedictionary implements idictionary, therefore every element add resource property must specify key. know using dictionarykeypropertyattribute, class can provide implicit key value - in case of datatemplate class, provided property "datatemplatekey". know sounds bit petty, motivation question know how use other classes if didnt have privilege see usage samples before (maybe 3rd party...). anyone?
as mentioned in question, entries without x:key
attribute use datatemplatekey(sometype) key. can specify 1 such instance particular sometype
in resources. datatemplatekey derived templatekey derived resourcekey. of course such datatemplate resource definitions can appear many types, staying unique, because datatemplatekey of each respective type unique.
for example, consider following resources definition:
<window.resources> <!-- generic button data template --> <datatemplate datatype="{x:type button}"> <!-- elements defining datatemplate--> </datatemplate> <!-- generic textblock data template --> <datatemplate datatype="{x:type textblock}"> <!-- elements defining datatemplate--> </datatemplate> <!-- specific button data template --> <datatemplate x:key="specialbutton" datatype="{x:type button}"> <!-- elements defining datatemplate--> </datatemplate> </window.resources>
this results in 3 entries in resource dictionary. orange arrows in image, below, point datatemplatekey
based entries button
, textblock
types, while red arrow points specific (keyed) entry specialbutton
keyed resource:
Comments
Post a Comment