public void Bind(ICollection<IDomainHierarchy> items, IDomainHierarchy target, bool addEmpty = false) { if (items == null) throw new ArgumentNullException("items"); _type = items.First().GetType().Name; _button.Text = target.IsNullOrEmpty() ? string.Format(_defaultText, _type) : target.Title; _button.CommandArgument = target.IsNullOrEmpty() ? string.Empty : target.Id.ToString(); AddEmpty(_tree, target, addEmpty); using (new ObjectScope<IDomainHierarchy>(items.ToList())) { var roots = items.Where(i => IsRoot(i, items)); foreach (var root in roots) Bind(_tree, root, target); } }
private static bool IsRoot(IDomainHierarchy target, ICollection<IDomainHierarchy> items) { if (target.IsNullOrEmpty()) return false; var parent = target.GetParent(); if (parent.IsNullOrEmpty()) return true; return !items.Contains(parent); }