Friday, 15 August 2014

silverlight - Partially templated ListBox.ItemTemplate -



silverlight - Partially templated ListBox.ItemTemplate -

i'm creating custom command , i'm trying create partially specified template list box items. template has predefined parts , there should part can templated when using control.

for have created dependency property named suggestionitemtemplate so:

public static readonly dependencyproperty suggestionitemtemplateproperty = dependencyproperty.register("suggestionitemtemplate", typeof(datatemplate), typeof(autosuggesttextbox), new propertymetadata(null));

in custom controls' generic.xaml have:

<style targettype="local:autosuggesttextbox"> <setter property="template"> <setter.value> <controltemplate targettype="local:autosuggesttextbox"> <grid> <listbox x:name="itemscontrol"> <listbox.itemspanel> <itemspaneltemplate> <stackpanel /> </itemspaneltemplate> </listbox.itemspanel> <listbox.itemtemplate> <datatemplate> <grid> <grid.columndefinitions> <columndefinition width="*" /> <columndefinition width="auto" /> </grid.columndefinitions> <contentpresenter grid.column="0" contenttemplate="{templatebinding suggestionitemtemplate}" content="{binding}" /> <togglebutton grid.column="1" x:name="detailshover" clickmode="hover" style="{staticresource detailstogglebuttonstyle}" /> </grid> </datatemplate> </listbox.itemtemplate> </listbox> </grid> </controltemplate> </setter.value> </setter> </style>

unfortunatelly, not work it's not possible utilize templatebinding within contentpresenter nested datatemplate. (the fellow member "suggestionitemtemplate" not recognized or not accessible.)

i tried utilize ancestor binding (available in silverlight 5) like:

<contentpresenter grid.column="0" contenttemplate="{binding relativesource={relativesource findancestor, ancestortype=local:autosuggesttextbox}, path=suggestionitemtemplate}" content="{binding}" />

but results in binding error:

error: system.exception: bindingexpression_cannotfindancestor

i suppose happens because i'm within controltemplate of custom command , "local:autosuggesttextbox" not defined anywhere in style.

the 3rd alternative tried apply contenttemplate in onapplytemplate override doesn't work:

var cp = itemscontrolelement.itemtemplate.loadcontent() contentpresenter; cp.contenttemplate = suggestionitemtemplate;

in cases, grid 2 columns, toggle button visible content presenter simple prints out view model's type name. (i believe default behavior if contenttemplate null).

is possible do? there other ways specify partial template , add together customized template part when necessary?

as workaround now, can specify

itemtemplate="{templatebinding suggestionitemtemplate}"

for list box , copy/paste generic template everywhere utilize control. behavior i'm hoping avoid in first place.

thanks!

edit: used code tags blocks of code, they're not highlighted reason. :/

it possible walk through visual ancestors in onapplytemplate method, find contentpresenter(s) , set itemtemplate on that. mind, fine single item, not much in itemscontrol scenario.

you accomplish after using own custom control. give content dependency property of type object, , template dp of type datatemplate (and multiples of 2 if fancy), , can set root visual style , templates in default style control.

in specific case, suggest best approach set togglebutton in listboxitem template instead customising listbox.itemcontainerstyle. easy modify default command template using look blend, , datacontext of togglebutton not change, changes own logic should minimal.

edit: if mean utilize number of different info templates, perhaps implicit info templates more suitable.

silverlight silverlight-5.0

No comments:

Post a Comment