c# - Control as dependencyproperty / WPF -


i have custom control derived control class. want create dependency property of control (for example, button) , place in controltemplate (so button can placed in xaml , mycontrol's users can subscribe it's events etc.). may tell me, how can it?

here result code example:

public class mycontrol: control {     static mycontrol( )     {         defaultstylekeyproperty.overridemetadata(typeof(mycontrol), new frameworkpropertymetadata(typeof(mycontrol)));     }      public static readonly dependencyproperty mybuttonproperty =         dependencyproperty.register("mybutton",                                     typeof(button),                                     typeof(mycontrol),                                     new propertymetadata(default(button)));      public button mybutton     {                 {             return (button) getvalue(mybuttonproperty);         }         set         {             setvalue(mybuttonproperty, value);         }     } } 

xaml:

<controltemplate targettype="{x:type lib:mycontrol}">         <canvas>                 <border child="{templatebinding mybutton}">                 </border>         </canvas> </controltemplate> 

your control's template can declare dependency on child controls via templatepartattribute. instance of dependency in onapplytemplate method.

[templatepart(name = partbutton, type = typeof(buttonbase))] public class mycontrol : control {     private const string partbutton = "part_button";     private buttonbase buttonpart;      public override void onapplytemplate()     {         base.onapplytemplate();          this.buttonpart = gettemplatechild(partbutton) buttonbase;     } } 

your control template like:

<style targettype="mycontrol">     <setter property="template">         <setter.value>             <controltemplate targettype="mycontrol">                 <border ...>                     <button x:name="part_button" .../>                 </border>             </controltemplate>         </setter.value>     </setter> </style 

note this.buttonpart null if template did not include appropriately named buttonbase within it. should strive ensure control still works when template parts missing.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -