wpf - how to know which treeview item is clicked using mvvm -


i having wpf mvvm application has treeview static items maintained in xaml page. how know in view-model menuitem clicked can show respective page accordingly.

    <treeview height="auto" horizontalalignment="stretch" margin="0" name="mytreeviewmenu"                        verticalalignment="stretch" width="auto" opacity="1"                       borderthickness="1" borderbrush="black" grid.row="2">          <treeviewitem header="country" width="auto" horizontalalignment="stretch"                        ></treeviewitem>          <treeviewitem header="view details" width="auto" horizontalalignment="stretch" isenabled="false">                 <treeviewitem header="user" />                 <treeviewitem header="group" />                 <treeviewitem header="user group" />             </treeviewitem>     </treeview> 

take @ treeview.selecteditem property page @ msdn.

you can bind directly treeview.selecteditem property:

<treeview itemssource="{binding items}" selecteditem="{binding item, mode=oneway}" /> 

note treeview.selecteditem property read only, must use oneway binding... means cannot set selected item view model. that, need create own 2 way selected item property using attached property.

edit >>>

my apologies @scroog1, use attachedproperty this. right oneway binding, there error using method. unfortuately, attachedproperty code long, there way this.

i wouldn't recommend it's never idea put ui properties data objects, if add isselected property data object, can bind directly treeviewitem.isselected property:

<treeview itemssource="items" horizontalalignment="stretch" ... name="mytreeviewmenu">     <treeview.itemcontainerstyle>         <style targettype="{x:type treeviewitem}">             <setter property="isselected" value="{binding isselected}" />         </style>     </treeview.itemcontainerstyle> </treeview> 

i searched , found 'fuller' answer in wpf mvvm treeview selecteditem post here on stackoverflow.

alternatively, there way... use treeview.selectedvalue , treeview.selectedvaluepath properties. basic idea set treeview.selectedvaluepath property name of property on data object. when item selected, treeview.selectedvalue property set value of property of selected data item. can find out more method how to: use selectedvalue, selectedvaluepath, , selecteditem page @ msdn. works best if have uniquely identifiable property identifier of kind. code example msdn:

<treeview itemssource="{binding source={staticresource myemployeedata},  xpath=employeeinfo}" name="mytreeview" selectedvaluepath="employeenumber" />  <textblock margin="10">selectedvaluepath: </textblock> <textblock margin="10,0,0,0" text="{binding elementname=mytreeview,  path=selectedvaluepath}" foreground="blue"/>  <textblock margin="10">selectedvalue: </textblock> <textblock margin="10,0,0,0" text="{binding elementname=mytreeview,  path=selectedvalue}" foreground="blue"/> 

Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -