c# - In WPF, Apply different DataTemplate to the same Item at a different level of TreeView -


kind of odd problem have here.

i have pretty basic recursive tree structure:

public class treenode {     public string name { get; set; }     public ienumerable<treenode> children { get; set; } } 

and displaying data in treeview using hierarchicaldatatemplate, so:

<treeview name="_tree">     <treeview.itemtemplate>         <hierarchicaldatatemplate itemssource="{binding children}">             <stackpanel orientation="horizontal" >                 <checkbox/>                 <textblock text="{binding path=name}"/>             </stackpanel>         </hierarchicaldatatemplate>     </treeview.itemtemplate> </treeview> 

i populate tree code behind:

//alltreenodes list of existing tree objects public void populatetree(list<treenode> alltreenodes) {     foreach (var node in alltreenodes)     {         _tree.items.add(node);     } } 

the result treeview each existing tree object root node respective subtrees.

note each treenode can displayed in tree @ multiple locations depending on whether or not has ancestors.

up point, works well, want have checkboxes displayed visual root nodes only. i've attempted using datatemplateselector 2 hierarchicaldatatemplates, selecting template based on additional boolean property of treenode, doesn't work because treenode needs displayed checkbox once , potentially multiple times without checkbox.

any appreciated.

edit: here dummy data explain want. (also note there no cyclic references in data.)

+treenodea   -treenodeb   -treenodec     -treenodeb +treenodeb +treenodec   -treenodeb +treenoded   -treenodee     -treenodec       -treenodeb +treenodee   -treenodec     -treenodeb 

in above view, nodes preceded + should have checkboxes.

well, don't know of straightforward approaches, create attached property , apply each treeviewitem element. see more implementing attached properties in overview , example on stackoverflow.

this attached property apply different style treeviewitem depending on level.

it bit tricky level unless you're programmatically producing treeviewitem hierarchy.

if not, either calculate every time registration of attached property call made traversing visual tree.

or try - should work in theory - doing relativesource binding attached property of parent , increment 1 using ivalueconverter:

<treeviewitem treeviewlevelstyle.level="{binding relativesource=          {relativesource mode=findancestor, ancestortype={x:type treeviewitem}},          converter=incrementbyonevalueconverter}"> 

(disclaimer - idea, , not tried previously)


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -