Advertisement
Guest User

Untitled

a guest
Aug 24th, 2014
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.67 KB | None | 0 0
  1. // provides unlimited amount of attached properties on demand
  2. class DependencyPropertyDepot : DependencyObject
  3. {
  4.     #region attached property int AllocatedPropertyCount
  5.     public static int GetAllocatedPropertyCount(DependencyObject obj)
  6.     {
  7.         return (int)obj.GetValue(AllocatedPropertyCountProperty);
  8.     }
  9.  
  10.     public static void SetAllocatedPropertyCount(DependencyObject obj, int value)
  11.     {
  12.         obj.SetValue(AllocatedPropertyCountProperty, value);
  13.     }
  14.  
  15.     public static readonly DependencyProperty AllocatedPropertyCountProperty =
  16.         DependencyProperty.RegisterAttached("AllocatedPropertyCount", typeof(int), typeof(DependencyPropertyDepot));
  17.     #endregion
  18.  
  19.     static List<DependencyProperty> existingProperties = new List<DependencyProperty>();
  20.  
  21.     static DependencyProperty GetDP(int index)
  22.     {
  23.         EnsureCount(index + 1);
  24.         return existingProperties[index];
  25.     }
  26.  
  27.     static void EnsureCount(int count)
  28.     {
  29.         if (existingProperties.Count < count)
  30.         {
  31.             for (int i = existingProperties.Count; i < count; i++)
  32.                 GenerateNewProperty(i);
  33.         }
  34.     }
  35.  
  36.     static void GenerateNewProperty(int index)
  37.     {
  38.         Debug.Assert(existingProperties.Count == index);
  39.         existingProperties.Add(DependencyProperty.RegisterAttached("Property" + index, typeof(object), typeof(DependencyPropertyDepot)));
  40.     }
  41.  
  42.     public static DependencyProperty AllocateNewPropertyFor(DependencyObject obj)
  43.     {
  44.         var idx = GetAllocatedPropertyCount(obj);
  45.         var dp = DependencyPropertyDepot.GetDP(idx);
  46.         SetAllocatedPropertyCount(obj, idx + 1);
  47.         return dp;
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement