.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> WPF >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Move Up/Down item in TreeView control in WPF

Posted By:Diptimaya Patra       Posted Date: May 22, 2010    Points: 25    Category: WPF    URL: http://www.dotnetspark.com  

Move Up/Down item in TreeView control in WPF. In this article we will see how we can move up/down a TreeViewItem in TreeView.
 

Creating A WPF Project

new.gif

Fire up Visual Studio 2008, create a WPF Application, and name it as TreeViewMoveUpDown.

In my previous article we have seen how to achieve Move Up and Move Down in a ListBox for a ListBoxItem. Now we would do the same for TreeView and TreeViewItem.

It's almost same, but due to the difference in Control type I am writing this sample.

Let's add a TreeView Control and Add Move Up and Move Down Buttons.

image2.gif

As you see from above image, I have added two static TreeViewItems such as Request 1 and Request 2.

The following is the XAML reference:

image3.gif

Code view of the above image

  
    
      
        
          
            
            
            
            
          
          
            
            
          
        
      
    
  
  


In the above XAML display, you can see the TreeViewItem's itemTemplate is customized.

Now we will load some sample data to the first node and see the result.

image4.gif

Code view of the above image
    #region Workflow
    public class Workflow
    {
        public int Rank { get; set; }
        public string Name { get; set; }
        public string AssignedTo { get; set; }
    }
    #endregion


Workflow class represents the structure of a Workflow.

image5.gif

Code View of the above image
        SortableObservableCollection sortedData;

        #region Constructor
        public Window1()
        {
            InitializeComponent();

            ObservableCollection workflowData = new ObservableCollection 
            {
                new Workflow{Rank=1, Name="Manager Approval", AssignedTo="Manager"},
                new Workflow{Rank=2, Name="Install Software 1", AssignedTo="System Engineer"},
                new Workflow{Rank=3, Name="Install Software 2", AssignedTo="System Engineer"},
                new Workflow{Rank=4, Name="Format System", AssignedTo="System Engineer"},
                new Workflow{Rank=5, Name="Software Approval", AssignedTo="Software Management"},
                new Workflow{Rank=6, Name="Admin Rights Approval", AssignedTo="Manager"},
                new Workflow{Rank=7, Name="Assign as Admin", AssignedTo="System Engineer"},
                new Workflow{Rank=8, Name="Give Feedback", AssignedTo="User"},
            };

            sortedData = new SortableObservableCollection(workflowData);
            sortedData.Sort(x => x.Rank, ListSortDirection.Ascending);

            foreach (Workflow item in sortedData)
            {
                tviRequest1.Items.Add(item);
            }
        } 
        #endregion


As you see in above code display we have added the items into the Request 1 node.

The following class is for using the SortableObservableCollection for sorting an ObservableCollection.

image6.gif

Codeview of the above image

    #region Sortable Observable Collection (Asc/Desc)
    public class SortableObservableCollection : ObservableCollection
    {
        public SortableObservableCollection(List list)
            : base(list) { }

        public SortableObservableCollection(IEnumerable collection)
            : base(collection) { }

        public void Sort(Func keySelector, ListSortDirection direction)
        {
            switch (direction)
            {
                case ListSortDirection.Ascending:
                    {
                        ApplySort(Items.OrderBy(keySelector));
                        break;
                    }
                case ListSortDirection.Descending:
                    {
                        ApplySort(Items.OrderByDescending(keySelector));
                        break;
                    }
            }
        }

        public void Sort(Func keySelector, IComparer comparer)
        {
            ApplySort(Items.OrderBy(keySelector, comparer));
        }

        private void ApplySort(IEnumerable sortedItems)
        {
            var sortedItemsList = sortedItems.ToList();
            foreach (var item in sortedItemsList)
            {
                Move(IndexOf(item), sortedItemsList.IndexOf(item));
            }
        }
    }
    #endregion

Now we will run the application to see the data loaded in TreeView.

image7.gif

Now we will achieve the Move Up and Move Down functionality.

This is almost same as we discussed in previous article for ListBoxItem Move Up and Move Down, but some changes.

Move Up

image8.gif
Codeview of the above image
        private void Move_Up(object sender, RoutedEventArgs e)
        {
            if (tvParent.SelectedItem != null)
            {
                if (tvParent.SelectedItem is Workflow)
                {
                    Workflow wf = tvParent.SelectedItem as Workflow;
                    if (wf.Rank != 1)
                    {
                        MoveUp(wf);
                        tviRequest1.Items.Clear();
                        sortedData.Sort(x => x.Rank, ListSortDirection.Ascending);
                        foreach (Workflow item in sortedData)
                        {
                            tviRequest1.Items.Add(item);
                        }
                    }
                }
            }
        }


The above code display is the Move Up Button click event handler, where we first check whether we have some item selected in the TreeView. Then we check if the SelectedItem is of type Workflow and if yes then we clear the items of the node and reload it.

image9.gif

Code view of the above image
        private void MoveUp(Workflow wf)
        {
            foreach (Workflow item in tviRequest1.Items)
            {
                if (item.Rank == wf.Rank - 1)
                {
                    item.Rank = wf.Rank;
                    break;
                }
            }
            wf.Rank--;
        } 


The above code is to iterate through the TreeViewItem's Items and change the Rank.

Move Down

Move Down functionality is the same but with Rank value is incremented.

image10.gif

Code view of the above image
        #region Move Down
        private void Move_Down(object sender, RoutedEventArgs e)
        {
            if (tvParent.SelectedItem != null)
            {
                if (tvParent.SelectedItem is Workflow)
                {
                    Workflow wf = tvParent.SelectedItem as Workflow;
                    if (wf.Rank != tviRequest1.Items.Count)
                    {
                        MoveDown(wf);
                        tviRequest1.Items.Clear();
                        sortedData.Sort(x => x.Rank, ListSortDirection.Ascending);
                        foreach (Workflow item in sortedData)
                        {
                            tviRequest1.Items.Add(item);
                        }
                    }
                }
            }
        }
        private void MoveDown(Workflow wf)
        {
            foreach (Workflow item in tviRequest1.Items)
            {
                if (item.Rank == wf.Rank + 1)
                {
                    item.Rank = wf.Rank;
                    break;
                }
            }
            wf.Rank++;
        }  
        #endregion


Now we will run the application to check the functionalities of Move Up and Move Down.

image11.gif

image12.gif

You can also download sample project used in the above example.

Hope this article helps.
 

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend