xaml - How to get {x:DataType} for a DataTemplate in C# for custom DataTemplateSelector -


i'm writing custom datatemplateselector combobox control , i'll need use display different datetemplates different kind of objects, in both closed , open modes combobox.

here's datatemplateselector came with:

public class comboboxtypeddatatemplateselector : datatemplateselector {     public ienumerable<datatemplate> selectedtemplates { get; set; }      public ienumerable<datatemplate> dropdowntemplates { get; set; }      protected override datatemplate selecttemplatecore(object item, dependencyobject container)     {         ienumerable<datatemplate> source = container.findparent<comboboxitem>() == null             ? selectedtemplates // template closed mode             : dropdowntemplates; // template open ui mode         type type = item.gettype();         return null; // linq first datatemplate in source {x:datatype} equals type     } }  public sealed class datatemplatescollection : list<datatemplate> { } 

and here's how i'd use in xaml:

<combobox>     <mvvm:comboboxtypeddatatemplateselector>         <mvvm:comboboxtypeddatatemplateselector.selectedtemplates>             <mvvm:datatemplatescollection>                 <datatemplate x:datatype="models:sometype">                     <textblock text="{x:bind ...}"/>                 </datatemplate>                 <datatemplate x:datatype="models:someothertype">                     <textblock text="{x:bind ...}"/>                 </datatemplate>             </mvvm:datatemplatescollection>         </mvvm:comboboxtypeddatatemplateselector.selectedtemplates>         <mvvm:comboboxtypeddatatemplateselector.dropdowntemplates>             <mvvm:datatemplatescollection>                 <datatemplate x:datatype="models:sometype">                     <textblock text="{x:bind ...}"/>                 </datatemplate>                 <datatemplate x:datatype="models:someothertype">                     <textblock text="{x:bind ...}"/>                 </datatemplate>             </mvvm:datatemplatescollection>         </mvvm:comboboxtypeddatatemplateselector.dropdowntemplates>     </mvvm:comboboxtypeddatatemplateselector> </combobox> 

now, piece of puzzle i'm missing, can't figure out how {x:datatype} property in c# (i know it's not real property, hope there's way retrieve via code). need able right datatemplate each object, right templates group. there way can achieve that?

note: know write specific datatemplateselector has hardcoded names of templates return each item type, , can use method fallback option. but, wondering if possible write more generic selector approach in order make more modular , able reuse in future.

thanks help!

edit: following suggestion vincent, wrote attached property store given type in datatemplate:

public class datatypehelper {     public static type getattacheddatatype(datatemplate element)     {         return (type)element.getvalue(attacheddatatypeproperty);     }      public static void setattacheddatatype(datatemplate element, type value)     {         element.setvalue(attacheddatatypeproperty, value);     }      public static readonly dependencyproperty attacheddatatypeproperty =         dependencyproperty.registerattached("attacheddatatype", typeof(type), typeof(datatypehelper), new propertymetadata(default(type))); } 

and i've tried use this:

...  <datatemplate x:datatype="somexlmns:someclass"                mvvm:datatypehelper.attacheddatatype="somexlmns:someclass">      ...  </datatemplate> 

but i'm getting xamlparseexception @ line set attached property type. i've tried set property "grid" (just test) , doesn't crash, don't understand why isn't working custom type.

edit #2: looks x:type markup extension not available in uwp , couldn't find way (i don't think it's possible @ all) type instance directly xaml, had use type name in xaml , compare item.gettype().name in template selector.

the ability assign type property directly xaml have been better it'd have had syntax/spell-check in xaml designer, @ least approach works fine.

you cannot retrieve value. hint compiler allow generate appropriate code binding.

you can either create custom attached property store need or use name property.

<local:selector x:key="selector" >     <local:selector.template1>         <datatemplate x:datatype="local:item" x:name="template1" >             <.../>         </datatemplate>     </local:selector.template1> </local:selector> 

then in selector implementation

template1.getvalue(frameworkelement.nameproperty); 

Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -