void InsertInternal(int index, XamlDesignItem item) { property.CollectionElements.Insert(index, item.XamlObject); if (CollectionChanged != null) { CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, index)); } NameScopeHelper.NameChanged(item.XamlObject, null, item.Name); }
void RemoveInternal(int index, XamlDesignItem item) { NameScopeHelper.NameChanged(item.XamlObject, item.Name, null); Debug.Assert(property.CollectionElements[index] == item.XamlObject); property.CollectionElements.RemoveAt(index); if (CollectionChanged != null) { CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item, index)); } }
/// <summary> /// Find's the Root XamlObject (real Root, or Root Object in Namescope) /// </summary> /// <param name="item"></param> /// <param name="onlyFromSameNamescope"></param> /// <returns></returns> internal static XamlObject GetRootXamlObject(XamlObject item, bool onlyFromSameNamescope = false) { var root = item; while (root.ParentObject != null) { if (onlyFromSameNamescope && NameScopeHelper.GetNameScopeFromObject(root) != NameScopeHelper.GetNameScopeFromObject(root.ParentObject)) { break; } root = root.ParentObject; } return(root); }
/// <summary> /// Get's all Child XamlObject Instances /// </summary> /// <param name="item"></param> /// <param name="onlyFromSameNamescope"></param> /// <returns></returns> private IEnumerable <XamlObject> GetAllChildXamlObjects(XamlObject item, bool onlyFromSameNamescope = false) { foreach (var prop in item.Properties) { if (prop.PropertyValue as XamlObject != null) { if (!onlyFromSameNamescope || NameScopeHelper.GetNameScopeFromObject(item) == NameScopeHelper.GetNameScopeFromObject(prop.PropertyValue as XamlObject)) { yield return(prop.PropertyValue as XamlObject); } foreach (var i in GetAllChildXamlObjects(prop.PropertyValue as XamlObject)) { if (!onlyFromSameNamescope || NameScopeHelper.GetNameScopeFromObject(item) == NameScopeHelper.GetNameScopeFromObject(i)) { yield return(i); } } } if (prop.IsCollection) { foreach (var collectionElement in prop.CollectionElements) { if (collectionElement as XamlObject != null) { if (!onlyFromSameNamescope || NameScopeHelper.GetNameScopeFromObject(item) == NameScopeHelper.GetNameScopeFromObject(collectionElement as XamlObject)) { yield return(collectionElement as XamlObject); } foreach (var i in GetAllChildXamlObjects(collectionElement as XamlObject)) { if (!onlyFromSameNamescope || NameScopeHelper.GetNameScopeFromObject(item) == NameScopeHelper.GetNameScopeFromObject(i)) { yield return(i); } } } } } } }
private static void AddToNamescopeRecursive(XamlDesignItem designItem) { NameScopeHelper.NameChanged(designItem.XamlObject, null, designItem.Name); foreach (var p in designItem.Properties) { if (p.Value != null) { AddToNamescopeRecursive((XamlDesignItem)p.Value); } else if (p.IsCollection && p.CollectionElements != null) { foreach (var c in p.CollectionElements) { AddToNamescopeRecursive((XamlDesignItem)c); } } } }
/// <summary> /// registers components from an existing XAML tree /// </summary> internal XamlDesignItem RegisterXamlComponentRecursive(XamlObject obj) { if (obj == null) { return(null); } foreach (XamlProperty prop in obj.Properties) { RegisterXamlComponentRecursive(prop.PropertyValue as XamlObject); foreach (XamlPropertyValue val in prop.CollectionElements) { RegisterXamlComponentRecursive(val as XamlObject); } } XamlDesignItem site = new XamlDesignItem(obj, _context); _context.Services.ExtensionManager.ApplyDesignItemInitializers(site); _sites.Add(site.Component, site); if (ComponentRegistered != null) { ComponentRegistered(this, new DesignItemEventArgs(site)); } if (_context.RootItem != null && !string.IsNullOrEmpty(site.Name)) { var nameScope = NameScopeHelper.GetNameScopeFromObject(((XamlDesignItem)_context.RootItem).XamlObject); if (nameScope != null) { // The object will be a part of the RootItem namescope, remove local namescope if set NameScopeHelper.ClearNameScopeProperty(obj.Instance); string newName = site.Name; if (nameScope.FindName(newName) != null) { int copyIndex = newName.LastIndexOf("_Copy", StringComparison.Ordinal); if (copyIndex < 0) { newName += "_Copy"; } else if (!newName.EndsWith("_Copy", StringComparison.Ordinal)) { string copyEnd = newName.Substring(copyIndex + "_Copy".Length); int copyEndValue; if (Int32.TryParse(copyEnd, out copyEndValue)) { newName = newName.Remove(copyIndex + "_Copy".Length); } else { newName += "_Copy"; } } int i = 1; string newNameTemplate = newName; while (nameScope.FindName(newName) != null) { newName = newNameTemplate + i++; } site.Name = newName; } nameScope.RegisterName(newName, obj.Instance); } } return(site); }