Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
XML
<TreeView Name="optionsTreeView">
                <TreeViewItem Header="View1"></TreeViewItem>
                <TreeViewItem Header="View2"></TreeViewItem>
                <TreeViewItem Header="View3"></TreeViewItem>
</TreeView>

in the above tree nodes i am trying to show respective windows..

How shall i bind the TreeviewItem Command to open a window..
for example On selection of first treenodeitem "View1" how can i open notepad 
Posted
Updated 29-Jun-11 22:25pm
v2

1 solution

There is a fundamental problem with this design as it stands because users expect clicking the treeview to expand nodes. Now, I appreciate that these are end nodes that you want to click on but, as it stands, the treeview is designed to expand on click.

So, how do you make it behave differently - and do it in a MVVM style, none-hacky, catching events way? Well, the trick is that you are clicking on the text in the header here. Does it have to be text though? Well, no it doesn't - you can change it. But, what do you change it to? Well, visually you want to indicate to the user that clicking on the text has some effect other than expanding a node, and to make things easy for yourself you want something with a Command that you can hook your command into. Your first thought might be to use and restyle a button, but this is a bit longwinded. Alternatively, and the one I'd go with, you could use a Hyperlink.

So, try this instead:
XML
<TreeViewItem>
  <TreeViewItem.Header>
    <TextBlock>
      <Hyperlink Command="TriggerNotepadCommand">
        Notepad
      <Hyperlink>
    </TextBlock>
  </TreeViewItem.Header>
<TreeViewItem>
Then, all you need to do is hook up the command code in your ViewModel.
 
Share this answer
 
Comments
Tarun.K.S 30-Jun-11 6:07am    
Good suggestion and well explained. 5+
arun_pk 30-Jun-11 6:17am    
Again Thanks Pete.. :)
i will try this..
My 5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900