c# - Changing HeaderTemplate in a treeview on IsSelected with multiple hierarchicaldatatemplates for different types -


i have treeview want add 2 different node types to, each type having own hierachicaldatatemplate. have working (code below)

what when node in tree selected, want template change node, different template boolnode nodes , different template comparenodes. have found examples using styles , triggers treeview, nodes share same template.

treeview xaml:

    <treeview name="m_ktest">         <treeview.resources>             <hierarchicaldatatemplate datatype="{x:type self:boolnode}" itemssource="{binding children}">                 <stackpanel orientation="horizontal">                     <textblock text="{binding optext}"/>                 </stackpanel>             </hierarchicaldatatemplate>             <hierarchicaldatatemplate datatype="{x:type self:comparenode}">                 <stackpanel orientation="horizontal">                     <textblock text="{binding header}"/>                     <textblock text=" "/>                     <textblock text="{binding optext}"/>                     <textblock text=" "/>                     <textblock text="{binding value}"/>                 </stackpanel>             </hierarchicaldatatemplate>         </treeview.resources>     </treeview> 

iquerynode:

public interface iquerynode {     observablecollection<iquerynode> children { get; }     int opindex { get; set; }     string optext{get;} } 

boolnode:

public class boolnode :iquerynode {     public int opindex { get; set; }     public string optext { { ... } }     public observablecollection<iquerynode> children { get; private set; }      public boolnode()     {         children = new observablecollection<iquerynode>();     } } 

comparenode:

public class comparenode: iquerynode {     public observablecollection<iquerynode> children { get; private set; }     public int opindex { get; set; }     public string optext {get {...} }     public string header { get; set; }     public string value { get; set; }      public comparenode()     {         children = new observablecollection<iquerynode>();     } } 

i got work in sort of "hacky" way.

i added "isselected" property 2 classes , bound isselected property of treeviewnode. then, since had 2 data types working with, added "isboolean" boolean field , triggered template change on 2 values:

treeview xaml:

    <treeview name="m_ktest">         <treeview.resources>             <hierarchicaldatatemplate x:key="booldisplaytemplate" datatype="{x:type self:boolnode}" itemssource="{binding children}"> /*template*/ </hierarchicaldatatemplate>             <hierarchicaldatatemplate x:key="booledittemplate" datatype="{x:type self:boolnode}" itemssource="{binding children}"> /*template*/ </hierarchicaldatatemplate>             <hierarchicaldatatemplate x:key="compareedittemplate"  datatype="{x:type self:comparenode}" itemssource="{binding children}"> /*template*/ </hierarchicaldatatemplate>             <hierarchicaldatatemplate x:key="comparedisplaytemplate" datatype="{x:type self:comparenode}" itemssource="{binding children}"> /*template*/ </hierarchicaldatatemplate>          <style targettype="{x:type treeviewitem}">             <setter property="isselected" value="{binding isselected, mode=twoway}" />             <style.triggers>                 <multidatatrigger>                     <multidatatrigger.conditions>                         <condition binding="{binding path=isboolnode}" value="true"/>                         <condition binding="{binding path=isselected}" value="false"/>                     </multidatatrigger.conditions>                     <multidatatrigger.setters>                         <setter value="{staticresource booldisplaytemplate}" property="headertemplate"/>                     </multidatatrigger.setters>                 </multidatatrigger>                  <multidatatrigger>                     <multidatatrigger.conditions>                         <condition binding="{binding path=isboolnode}" value="true"/>                         <condition binding="{binding path=isselected}" value="true"/>                     </multidatatrigger.conditions>                     <multidatatrigger.setters>                         <setter value="{staticresource booledittemplate}" property="headertemplate"/>                     </multidatatrigger.setters>                 </multidatatrigger>                  <multidatatrigger>                     <multidatatrigger.conditions>                         <condition binding="{binding path=isboolnode}" value="false"/>                         <condition binding="{binding path=isselected}" value="false"/>                     </multidatatrigger.conditions>                     <multidatatrigger.setters>                         <setter value="{staticresource comparedisplaytemplate}" property="headertemplate"/>                     </multidatatrigger.setters>                 </multidatatrigger>                  <multidatatrigger>                     <multidatatrigger.conditions>                         <condition binding="{binding path=isboolnode}" value="false"/>                         <condition binding="{binding path=isselected}" value="true"/>                     </multidatatrigger.conditions>                     <multidatatrigger.setters>                         <setter value="{staticresource compareedittemplate}" property="headertemplate"/>                     </multidatatrigger.setters>                 </multidatatrigger>             </style.triggers>         </style>         </treeview.resources>     </treeview> 

iquerynode:

public interface iquerynode {     observablecollection<iquerynode> children { get; }      int opindex { get; set; }      string optext{get;}     bool isboolnode { get; }     bool isselected { get; set; } } 

boolnode:

public class boolnode :iquerynode {     public int opindex { get; set; }     public string optext { { ... } }     public observablecollection<iquerynode> children { get; private set; }     public bool isboolnode{get{return true;}}     public bool isselected { get; set;}      public boolnode()     {         opindex = 0;         children = new observablecollection<iquerynode>();         isselected = false;     } } 

comparenode:

public class comparenode: iquerynode {     public observablecollection<iquerynode> children { get; private set; }     public int opindex { get; set; }     public string optext{ get{ ... } }     public string header { get; set; }     public string value { get; set; }     public bool isboolnode { { return false; } }     public bool isselected { get; set; }     public comparenode()     {         children = new observablecollection<iquerynode>();         isselected = false;     } } 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -