TreeViewItem.Expanded

前端 未结 2 1988
清歌不尽
清歌不尽 2021-01-14 23:36

Im using the latest version of mvvm light toolkit, however i\'m not clear how I can use EventToCommand for the event TreeViewItem.Expanded.

THis dosent work... what

2条回答
  •  余生分开走
    2021-01-15 00:24

    I was able to do this by creating a custom ItemContainerStyle for the TreeView. You should be able to put this together with the following code snippets.

    1. A ViewModel for the TreeView

    public class TreeViewModelView
    {
        public IEnumerable Values
        {
            get { /* return hierarchical data source here ... */ }
        }
    
        /// 
        /// Command executed when the TreeViewItem expanding event is raised. The data item is passed in as a parameter.
        /// 
        public RelayCommand ExpandedCommand
        {
            get { return new RelayCommand( o => MessageBox.Show( o.GetType().Name ) ); }
        }
    
        /// 
        /// Command executed when the TreeViewItem collapsing event is raised.
        /// 
        public RelayCommand CollapsedCommand
        {
            get { return new RelayCommand( () => MessageBox.Show( "Collapsed" ) ); }
        }
    
    }
    
    
    

    2. Define the TreeView and setup the required data binding:

     
        
            
    
            
                
                    
                
            
        
    
    

    3. In Expression Blend you can create edit a copy of the ItemContainerStyle template

    
    

    4. Then replace the ToggleButton with:

    
    
        
            
            
                
            
            
            
                
            
        
    
    
    

    提交回复
    热议问题