c# - How to parse Children of TreeviewItem correctly in ViewModel? -


i have treeview control in wpf program,which gets data mysql server , show databases , tables:

server ...databases     ...tables 

what want when click item in treeview,i can server/databases/tables' name.however,after hours testing,i still not name.


hierarchicaldatatemplate of server

<hierarchicaldatatemplate       datatype="{x:type local:serverviewmodel}"        itemssource="{binding children}">      <stackpanel orientation="horizontal">         <image width="16" height="16" margin="3,0" source="figures/server.png" />         <textblock text="{binding servername}" />     </stackpanel> </hierarchicaldatatemplate> 

the other 2 similar, except hierarchicaldatatemplate of table not have itemssource.

as i'm using mvvmlight pattern,i pass selecteditem commandparameterto viewmodel:

<i:interaction.triggers>     <i:eventtrigger eventname="selecteditemchanged">         <cmd:eventtocommand             command="{binding tableselected}"             commandparameter="{binding selecteditem,elementname=mytreeview}"             passeventargstocommand="true"/>     </i:eventtrigger> </i:interaction.triggers> 

in viewmodel,i delared command handle selecteditemchanged event.i implemented execute() method below,yet did not work.whenever click treeviewitem,an unhandled exception system.reflection.targetinvocationexception thrown.

private void getselecteditem(object parameter) {     var item = parameter treeviewitem;     stackpanel stackpanel = (stackpanel)visualtreehelper.getchild(item, 0);     textblock textblock = (textblock)visualtreehelper.getchild(stackpanel, 1);     messagebox.show(textblock.text); } 

update


command

public icommand tableselected { get; private set; }//in viewmodel 

tableselected = new relaycommand<object>((obj) => getselecteditem(obj), (obj) => true); //implemented in viewmodel's constructor 

i've read several related posts on stackoverflow,like how can content of treeviewitem in wpf?,wpf: getting treeviewitem's constituent controls,yet still can not right.please help,thanks.

i realized doing totally wrong.the selecteditem not collections of ui control,but purely instance same type of datatype.

<hierarchicaldatatemplate      datatype="{x:type local:databaseviewmodel}"      itemssource="{binding children}">      <stackpanel orientation="horizontal">         <image width="16" height="16" margin="3,0" source="figures/database.png" />         <textblock text="{binding databasename}" />     </stackpanel> </hierarchicaldatatemplate> 

the code above part of treeview.if click treeviewitem uses template,what databaseviewmodel instance,rather stackpanel or children.this misleading wpf beginners.


Comments

Popular posts from this blog

android - Automated my builds -

how to proxy from https to http with lighttpd -

python - Flask migration error -