Click here to Skip to main content
15,896,063 members
Articles / Desktop Programming / WPF

WPF Breadcrumb Folder TextBox

Rate me:
Please Sign up or sign in to vote.
4.41/5 (10 votes)
15 Jan 2009LGPL36 min read 101.2K   1.7K   71  
This article provides an implementation of a WPF Breadcrumb control, and describes how to develop one.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using QuickZip.UserControls.Breadcrumb.Framework;
using System.Windows.Media;

namespace QuickZip.UserControls.Breadcrumb.Presenter
{
    public class BreadcrumbCorePresenter : PresenterBase<View.BreadcrumbCoreView>
    {
        private bool _trailDirEnabled = false;
        internal BreadcrumbItemBase _root;        

        public BreadcrumbItemBase Root { get { return _root; } } 
        //public BreadcrumbItemBase SelectedItem { get { return View.LastNonTailItem; } }
        public string SelectedPath { get { return View.SelectedFolder; } }
        public string SelectedDisplayPath { get { return View.LastNonTailItem.FolderDisplayPath; } set { ChangeFolder(BreadcrumbItemBase.PathType.ptDisplay, value); } }
        public ImageSource Icon { get { return View.LastNonTailItem.Icon; } }
        public bool TrailDirEnabled { set { _trailDirEnabled = value; } }

        internal void ChangeFolder(BreadcrumbItemBase.PathType pt, string folderPath)
        {
            //if (!View.GetRootLoaded()) return;

            /* Can only trace path using display path, so convert first. */
            if (pt == BreadcrumbItemBase.PathType.ptFolder)
                folderPath = Root.ConvertPath(pt, folderPath);
            if (folderPath.StartsWith(Root.DisplayName))
                folderPath = folderPath.Substring(Math.Min(Root.DisplayName.Length + 1, folderPath.Length));

            /* Do the rest */

            /* TrailDir */
            BreadcrumbItemBase newFolder = Root.LookupFolder(pt, folderPath);
            if (View.SelectedPresenter == newFolder) return;

            if (newFolder != null &&  _trailDirEnabled && newFolder.FolderLevel > 0 && newFolder != null && newFolder.IsVisible && View.LastItem.HasParent(newFolder))
            {
                BreadcrumbItemBase[] hierarchy = View.LastItem.Hierarchy;
                for (int i = 0; i < View.LastItem.FolderLevel + 1; i++)                
                    hierarchy[i].IsTrailDir = false;
                hierarchy[newFolder.FolderLevel].ClearSelection();
                for (int i = newFolder.FolderLevel + 1; i <= View.LastItem.FolderLevel; i++)
                    hierarchy[i].IsTrailDir = true;
            }
            else
            /* */
            {
                Root.ClearSelection();
                Root.ExpanderClear();
                if (folderPath == null) { View.ClearAll(); } // Null -> Select Root
                else
                {
                    string[] folderSplit = folderPath.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);                    
                    string current;
                    BreadcrumbItemBase currentPresenter = Root;
                    View.ClearAfterIndex(folderSplit.Length); //It wont be displayed anyway, remove them.       

                    for (int i = 0; i < folderSplit.Length; i++)
                    {
                        current = folderSplit[i].EndsWith(":") ? folderSplit[i] + "\\" : folderSplit[i]; // e.g. C: ---> C:\
                        string next = i + 1 < folderSplit.Length ? folderSplit[i + 1] : ""; // next select path                    

                        if (View.Count > i && (current == View.GetItem(i).DisplayName || current.TrimEnd('\\') == View.GetItem(i).FolderName)) //Check if the path is exists and same
                        {
                            //Ignore it, already constructed
                            View.GetItem(i).ClearSelection(); //Reset select folder
                            View.GetItem(i).IsTrailDir = false;
                        }
                        else
                        {
                            //Construct it.
                            View.ClearAfterIndex(i);
                            BreadcrumbItemBase nextPresenter = currentPresenter[BreadcrumbItemBase.PathType.ptDisplay, current];
                            if (nextPresenter == null)
                                throw new ArgumentException("Path not found");
                            else
                                if (next != "") nextPresenter.SelectedDisplayFolder = next;
                            nextPresenter.IsTrailDir = false;
                            this.View.AddItem(nextPresenter);
                        }
                        currentPresenter = View.GetItem(i);
                    }

                     
                }
            }
            Root.ShowDisplayName = View.Count == 0;

            View.SelectedFolder = Root.ConvertPath(BreadcrumbItemBase.PathType.ptDisplay, folderPath);
            View.SelectedPresenter = View.LastNonTailItem;
            

            //OnPropertyChanged("SelectedItem");
            //OnPropertyChanged("SelectedPath");
            OnPropertyChanged("SelectedDisplayPath");
            OnPropertyChanged("Icon");
        }

        

        public BreadcrumbCorePresenter(View.BreadcrumbCoreView view)
            : base(view)
        {
        }


        internal void SetRootItem(BreadcrumbItemBase newRoot)
        {
            View.SetRootItem(newRoot);
            OnPropertyChanged("Icon");
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Founder
Hong Kong Hong Kong

Comments and Discussions