c# - Initialize Collection of DataTemplates in XAML -


i have dependencyproperty

public observablecollection<datatemplate> wizardtemplatecollection {     { return (observablecollection<datatemplate>)getvalue(wizardtemplatecollectionproperty); }     set { setvalue(wizardtemplatecollectionproperty, value); } }  // using dependencyproperty backing store myproperty.  enables animation, styling, binding, etc... public static readonly dependencyproperty wizardtemplatecollectionproperty =     dependencyproperty.register("wizardtemplatecollection", typeof(observablecollection<datatemplate>), typeof(customwizardcontrol), new propertymetadata(new observablecollection<datatemplate>())); 

and want this:

<custom:customwizardcontrol>     <custom:customwizardcontrol.wizardtemplatecollection>         <datatemplate>             <rectangle></rectangle>         </datatemplate>         <datatemplate>             <rectangle></rectangle>         </datatemplate>         <datatemplate>             <rectangle></rectangle>         </datatemplate>     </custom:customwizardcontrol.wizardtemplatecollection> </custom:customwizardcontrol> 

what datatype need? or how can initialize observablecollection in xaml.

additional:

public class customwizardcontrol : control {} 

your customwizardcontrol class must inherit depenedencyobject or 1 of derived types example uielement or control:

public class customwizardcontrol : control {     public observablecollection<datatemplate> wizardtemplatecollection     {         { return (observablecollection<datatemplate>)getvalue(wizardtemplatecollectionproperty); }         set { setvalue(wizardtemplatecollectionproperty, value); }     }     ... } 

this works:

public class customwizardcontrol : control {     public customwizardcontrol()     {         wizardtemplatecollection = new observablecollection<datatemplate>();     }      public observablecollection<datatemplate> wizardtemplatecollection     {         { return (observablecollection<datatemplate>)getvalue(wizardtemplatecollectionproperty); }         set { setvalue(wizardtemplatecollectionproperty, value); }     }      public static readonly dependencyproperty wizardtemplatecollectionproperty =         dependencyproperty.register("wizardtemplatecollection", typeof(observablecollection<datatemplate>), typeof(customwizardcontrol), new propertymetadata(null)); } 

<local:customwizardcontrol x:name="ctrl">     <local:customwizardcontrol.wizardtemplatecollection>         <datatemplate>             <rectangle></rectangle>         </datatemplate>         <datatemplate>             <rectangle></rectangle>         </datatemplate>         <datatemplate>             <rectangle></rectangle>         </datatemplate>     </local:customwizardcontrol.wizardtemplatecollection> </local:customwizardcontrol> 

<textblock text="{binding wizardtemplatecollection.count, elementname=ctrl}" /> 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -