Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // provides unlimited amount of attached properties on demand
- class DependencyPropertyDepot : DependencyObject
- {
- #region attached property int AllocatedPropertyCount
- public static int GetAllocatedPropertyCount(DependencyObject obj)
- {
- return (int)obj.GetValue(AllocatedPropertyCountProperty);
- }
- public static void SetAllocatedPropertyCount(DependencyObject obj, int value)
- {
- obj.SetValue(AllocatedPropertyCountProperty, value);
- }
- public static readonly DependencyProperty AllocatedPropertyCountProperty =
- DependencyProperty.RegisterAttached("AllocatedPropertyCount", typeof(int), typeof(DependencyPropertyDepot));
- #endregion
- static List<DependencyProperty> existingProperties = new List<DependencyProperty>();
- static DependencyProperty GetDP(int index)
- {
- EnsureCount(index + 1);
- return existingProperties[index];
- }
- static void EnsureCount(int count)
- {
- if (existingProperties.Count < count)
- {
- for (int i = existingProperties.Count; i < count; i++)
- GenerateNewProperty(i);
- }
- }
- static void GenerateNewProperty(int index)
- {
- Debug.Assert(existingProperties.Count == index);
- existingProperties.Add(DependencyProperty.RegisterAttached("Property" + index, typeof(object), typeof(DependencyPropertyDepot)));
- }
- public static DependencyProperty AllocateNewPropertyFor(DependencyObject obj)
- {
- var idx = GetAllocatedPropertyCount(obj);
- var dp = DependencyPropertyDepot.GetDP(idx);
- SetAllocatedPropertyCount(obj, idx + 1);
- return dp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement