Click here to Skip to main content
15,886,791 members
Articles / Desktop Programming / WPF

Tuning Up The TreeView - Part 2

Rate me:
Please Sign up or sign in to vote.
4.75/5 (7 votes)
2 Mar 2010CPOL11 min read 41.9K   1.6K   40  
Improved TreeView sorting, filtering, selection, and efficiency.
using System;

using DirectoryTree.VM;

namespace DirectoryTree.Test
{
   // Test Data Generator
   public static class DataFactory
   {
      private static int driveCount = 1;

      // Populate a Drive hierarchy
      public static Drive CreateDrive()
      {
         Drive drive = new Drive(null, "Drive" + driveCount++);

         for (int i = 0; i < 8; i++)
         {
            Directory dir = CreateDir(drive, false);
            for (int j = 0; j < i; j++)
               CreateDir(dir, false);
         }
         return drive;
      }

      // partial = true  - construct a partial directory without size
      //         = false - fully construct
      public static Directory CreateDir(TreeNode parent, bool partial)
      {
         Directory dir = new Directory(parent, RandomDirName() );
         if (!partial)
            dir.Size = RandomDirSize();
         return dir;
      }

      private static Random gen = new Random(32767);
      public static int RandomInt(int max) { return gen.Next(max); }

      public static int RandomDirSize()
      {
         int size = RandomInt(10000);
         if (size % 11 == 0) size = 0; // create more empties
         return size;
      }

      private const string letters = "AaBbCcDdEeFfGgHhIiJjKkLlMm";
      private static int seq;
      private static string RandomDirName()
      {
         return "Dir" +
              letters.Substring(RandomInt(letters.Length / 2) * 2, 2) +
                  seq++.ToString("D3");

      }
   }
}

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 Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
Lee has worked on user interfaces, graphics, computational geometry, memory management, threading, and assorted applications in C#, Java, C++, and C. He started out programming in Fortran on a 128 Kb PDP/11, which only proves that he's old, not smart. Lee also writes about chronic illness and his love of animals; his auto racing related articles are here.

Comments and Discussions