.net - WPF CustomControl pass through of datatemplate to listbox not working -
i learning custom controls , making autocompletetextbox example. creating custom control wpf project (v 4.5 vb.net 4.5) , using textbox base class. added popup, listbox, , button control. have dependency property in custom control datatemplate of listbox, can't datatemplate pssed listbox.
here dependency property datatemple:
#region "dependency properties -- itemtemplate" public property itemtemplate datatemplate return getvalue(itemtemplateproperty) end set(byval value datatemplate) setvalue(itemtemplateproperty, value) end set end property public shared readonly itemtemplateproperty dependencyproperty = dependencyproperty.register( _ "itemtemplate", gettype(datatemplate), gettype(autocompletetextbox), _ new frameworkpropertymetadata(nothing, frameworkpropertymetadataoptions.none, _ new propertychangedcallback(addressof onitemtemplatechanged))) shared sub onitemtemplatechanged(d dependencyobject, e dependencypropertychangedeventargs) dim actb autocompletetextbox = trycast(d, autocompletetextbox) if actb isnot nothing dim temptemplate datatemplate = trycast(e.newvalue, datatemplate) if temptemplate isnot nothing actb.itemtemplate = temptemplate end if end if end sub #end region here xaml declaring small text of usercontrol:
<window x:class="mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:krisis="clr-namespace:krisis.controls;assembly=krisis.controls" title="mainwindow" height="350" width="525" x:name="mywindow" datacontext="{binding relativesource={relativesource self}}"> <window.resources> <datatemplate x:key="collectiontemplate"> <border borderbrush="green" borderthickness="2" cornerradius="5" padding="5,5,5,2"> <grid> <grid.columndefinitions> <columndefinition width="auto"/> <columndefinition/> </grid.columndefinitions> <grid.rowdefinitions> <rowdefinition/> <rowdefinition/> </grid.rowdefinitions> <textblock text="object: "/> <textblock grid.column="1" text="{binding name}"/> <textblock grid.row="1" text="{binding id}"/> <textblock grid.column="1" grid.row="1" text="{binding job}"/> </grid> </border> </datatemplate> </window.resources> <grid> <krisis:autocompletetextbox itemssource="{binding collection}" itemtemplate="{staticresource collectiontemplate}" maxmimummatches="15" minimumfiltercharacters="1" displaypath="name" width="497" minheight="35" fontsize="18" horizontalalignment="center" verticalalignment="center" margin="10,41,10,243" /> </grid> </window> my problem: when use declare itemtemplate, not getting applied. listbox displays object type name each object in listbox, not of object property values.
can me use dependency property pass through datatemplate of listbox inside custom control.
thanks in advance
okay, chnaged assign itemtemplate following , works:
#region "apply template" public overrides sub onapplytemplate() mybase.onapplytemplate() '' if template not nothing initialize controls , wire event handlers if me.template isnot nothing initializelistbox() if resultslistbox isnot nothing onitemsourcechanged(itemssource) addhandler resultslistbox.keydown, addressof resultlistbox_keydown addhandler resultslistbox.selectionchanged, addressof resultlistbox_selectionchanged resultslistbox.itemtemplate = itemtemplate resultslistbox.itemssource = itemssource end if end if end sub #end region
Comments
Post a Comment