none
How to expand a TreeView node by one clicking on it?

    Question

  • I want to expand the treeview node by one clicking on it, just like the MSN contact list do.

    How to realize it? Could you give me any suggestion?


    Thanks!
    Wednesday, March 18, 2009 12:32 PM

Answers

  • <TreeViewItem Click="OnItemClick"
    and in event service method
    public void OnItemClick(object sender, ...) 
        (sender as TreeViewItem).IsExpanded = true
    that's all u need to do

    and for all nodes u could write a style

    <TreeView> 
       <TreeView.Resources> 
           <Style TargetType={x:Type TreeViewItem}> 
               <EventSetter Event="" Handler="OnItemClick"/> 
           </Style> 
       </TreeView.Resources> 
       <TreeViewItem> 
       <TreeViewItem> 
       <TreeViewItem> 
          <TreeViewItem> 
          <TreeViewItem> 
       </TreeViewItem> 
    </TreeView> 

    • Edited by DotNetDancer Wednesday, March 18, 2009 1:24 PM more info
    • Marked as answer by zhaoxb1982 Thursday, March 19, 2009 1:37 AM
    • Unmarked as answer by zhaoxb1982 Monday, March 23, 2009 9:21 AM
    • Marked as answer by Tao Liang Tuesday, March 24, 2009 2:34 AM
    Wednesday, March 18, 2009 1:18 PM

All replies

  • <TreeViewItem Click="OnItemClick"
    and in event service method
    public void OnItemClick(object sender, ...) 
        (sender as TreeViewItem).IsExpanded = true
    that's all u need to do

    and for all nodes u could write a style

    <TreeView> 
       <TreeView.Resources> 
           <Style TargetType={x:Type TreeViewItem}> 
               <EventSetter Event="" Handler="OnItemClick"/> 
           </Style> 
       </TreeView.Resources> 
       <TreeViewItem> 
       <TreeViewItem> 
       <TreeViewItem> 
          <TreeViewItem> 
          <TreeViewItem> 
       </TreeViewItem> 
    </TreeView> 

    • Edited by DotNetDancer Wednesday, March 18, 2009 1:24 PM more info
    • Marked as answer by zhaoxb1982 Thursday, March 19, 2009 1:37 AM
    • Unmarked as answer by zhaoxb1982 Monday, March 23, 2009 9:21 AM
    • Marked as answer by Tao Liang Tuesday, March 24, 2009 2:34 AM
    Wednesday, March 18, 2009 1:18 PM
  • Hi DotNetDancer,

    Thanks for your reply.

    According to your solution, need I add a flag to indicate whether the node has been expanded, and I should shrink the node if it has been expanded when click on it again?

    I want to know if there any ways to realize this in XAML, not it procedure code.

    Thanks!

    • Proposed as answer by DotNetDancer Thursday, March 19, 2009 9:31 AM
    Wednesday, March 18, 2009 1:24 PM
  • Flag IsExpanded is a  buildIn Dependency Property in TreeViewItem

    about procedural code, sorry, but i don't know "only xaml" solution...
    Wednesday, March 18, 2009 2:54 PM
  • Hi DotNetDancer,

    I think this is one solution to the problem.

    I will have a try.


    Thanks!
    Thursday, March 19, 2009 1:34 AM
  • is u builds TreeView dynamiclly (via ItemsSource property) in OnItemClick method use

    public void OnItemClick(object sender, ....) 
        treeViewItem = TreeViewName.ItemsContainerGenrator.ContainerFromItem(sender); 
        treeViewItem.IsExpander = true
     where treeViewName is name for TreeView (<TreeView x:Name="treeViewName".../>)

    Thursday, March 19, 2009 9:41 AM
  • Hi DotNetDancer,

    I could not find ItemClick event in TreeView and TreeViewItem Members.

    Maybe I have to find another way to handle this.
    Monday, March 23, 2009 9:23 AM