Monday, 15 March 2010

wpf - Need to use an inner data Template that is the same as outer template -



wpf - Need to use an inner data Template that is the same as outer template -

i have next xml structure(simplified example's sake) can have questions within questions. there other types of questions have decided create template display them, can create top level of questions show, when inner levels can't template show. have other question types other "string", removed them seek focus on issue @ hand.

the xml structure

<questions> <question id="3" text="what this?" type="string"> <questions> <question id="7" text="what first inner question?" type="string"> <questions> <question id="8" text="what inner question?" type="string"> </question> </questions> </question> </questions> </question> <question id="4" text="where question?" type="string"> <questions> <question id="5" text="what inner question?" type="string"> <questions> <question id="6" text="what inner question?" type="string"> </question> </questions> </question> </questions> </question> </questions>

my xaml

<window.resources> <datatemplate x:key="stringquestiontemplate" datatype="models:formquestion" > <stackpanel orientation="horizontal" > <textblock text="{binding path=questiontext}" /> <textbox text="{binding path=answer, mode=twoway}" /> <stackpanel> <itemscontrol itemssource="{binding path=innerquestions}" itemtemplateselector="{staticresource templateselector}" /> </stackpanel> </stackpanel> </datatemplate> <templateissue:questiontemplateselector x:key="templateselector" stringtemplate="{ staticresource stringquestiontemplate }" /> </window.resources> <grid> <stackpanel orientation="vertical"> <itemscontrol itemssource="{binding path=questions}" itemtemplateselector="{staticresource templateselector}" > </itemscontrol> </stackpanel> </grid>

here questiontemplateselector

public class questiontemplateselector : datatemplateselector { public datatemplate stringtemplate { get; set; } public override datatemplate selecttemplate(object item, dependencyobject container) { switch (((formquestion)item).type) { case questiontype.stringquestion: homecoming stringtemplate; default: homecoming stringtemplate; } } }

i utilize same template selector "innerquestions" original templates, since referenced after section not able to(an error thrown). utilize same datatemplate in "innerquestions" top level questions. there way accomplish this, either templating using different structure?

it sounds need utilize hierarchicaldatatemplate class. perhaps (assuming bindings correct):

<hierarchicaldatatemplate datatype="models:formquestion" itemssource="{binding path=innerquestions}" itemtemplateselector="{staticresource templateselector}"> <stackpanel orientation="horizontal" > <textblock text="{binding path=questiontext}" /> <textbox text="{binding path=answer, mode=twoway}" /> </stackpanel> </datatemplate>

update >>>

you need utilize command can display hierarchical data, treeview:

<treeview itemssource="{binding toplevelquestions}" />

wpf datatemplate

No comments:

Post a Comment