C#ã§ã¯List
親åãªæ¹ã以ä¸ã®ããªã¼ã³ã¬ã¯ã·ã§ã³ãç¨æãã¦ããã®ã§ããã¦ã³ãã¼ããã¦ä½¿ã£ã¦ã¿ãã¨ãããããªãé«æ©è½ãªã®ã§ããç¶æ¿ãã¦ä½¿ç¨ããã«ã¯asã§ãã£ã¹ãããããã¦é¢åã§ããã£ã±ãèªåã§å®è£ ããæ¹ãè¯ãã¨ãããã¨ã§ãå¿ è¦æå°éã®ããªã¼æ§é ã¯ã©ã¹ãä½æãã¦ã¿ã¾ãããã³ã¡ã³ãã¯Visual Studioã®è¨æ³ã§ãã
http://www.codeproject.com/Articles/12476/A-Generic-Tree-Collection
ã¾ãã¯ã¤ã³ã¿ã¼ãã§ã¼ã¹ITreeNode.cs(ITreeNode
/// <summary> /// ããªã¼æ§é ã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ /// </summary> public interface ITreeNode<T> { T Parent { get; set; } IList<T> Children { get; set; } T AddChild(T child); T RemoveChild(T child); bool TryRemoveChild(T child); T ClearChildren(); bool TryRemoveOwn(); T RemoveOwn(); }
ãã®å®è£
ã¯ã©ã¹TreeNodeBase.cs(TreeNodeBase
/// <summary> /// ç°¡æããªã¼æ§é ã®ã¸ã§ããªãã¯ã¯ã©ã¹ /// </summary> /// <typeparam name="T"></typeparam> public abstract class TreeNodeBase<T> : ITreeNode<TreeNodeBase<T>> where T : class { /// <summary> /// 親ã¸ã®åç §ãã£ã¼ã«ã /// </summary> protected TreeNodeBase<T> parent = null; /// <summary> /// 親ã¸ã®åç §ãããã㣠/// </summary> public virtual TreeNodeBase<T> Parent { get { return parent; } set { parent = value; } } /// <summary> /// åãã¼ãã®ãªã¹ããã£ã¼ã«ã /// </summary> protected IList<TreeNodeBase<T>> children = null; /// <summary> /// åãã¼ãã®ãªã¹ããããã㣠/// </summary> public virtual IList<TreeNodeBase<T>> Children { get { if (children == null) children = new List<TreeNodeBase<T>>(); return children; } set { children = value; } } /// <summary> /// åãã¼ãã追å ããã /// </summary> /// <param name="child">追å ããããã¼ã</param> /// <returns>追å å¾ã®ãªãã¸ã§ã¯ã</returns> public virtual TreeNodeBase<T> AddChild(TreeNodeBase<T> child) { if (child == null) throw new ArgumentNullException("Adding tree child is null."); this.Children.Add(child); child.Parent = this; return this; } /// <summary> /// åãã¼ããåé¤ããã /// </summary> /// <param name="child">åé¤ããããã¼ã</param> /// <returns>åé¤å¾ã®ãªãã¸ã§ã¯ã</returns> public virtual TreeNodeBase<T> RemoveChild(TreeNodeBase<T> child) { this.Children.Remove(child); return this; } /// <summary> /// åãã¼ããåé¤ããã /// </summary> /// <param name="child">åé¤ããããã¼ã</param> /// <returns>åé¤ã®å¯å¦</returns> public virtual bool TryRemoveChild(TreeNodeBase<T> child) { return this.Children.Remove(child); } /// <summary> /// åãã¼ããå ¨ã¦åé¤ããã /// </summary> /// <returns>åãã¼ããå ¨åé¤å¾ã®ãªãã¸ã§ã¯ã</returns> public virtual TreeNodeBase<T> ClearChildren() { this.Children.Clear(); return this; } /// <summary> /// èªèº«ã®ãã¼ãã親ããªã¼ããåé¤ããã /// </summary> /// <returns>親ã®ãªãã¸ã§ã¯ã</returns> public virtual TreeNodeBase<T> RemoveOwn() { TreeNodeBase<T> parent = this.Parent; parent.RemoveChild(this); return parent; } /// <summary> /// èªèº«ã®ãã¼ãã親ããªã¼ããåé¤ããã /// </summary> /// <returns>åé¤ã®å¯å¦</returns> public virtual bool TryRemoveOwn() { TreeNodeBase<T> parent = this.Parent; return parent.TryRemoveChild(this); } }
éç¥åã®ããªã¼æ§é ãä½æããå ´åã¯
Observableãªã¯ã©ã¹ãä½æãããå ´åã¯ããã®ã¯ã©ã¹ãç¶æ¿ãã¦ããããã£ãoverrideããããç´æ¥ITreeNode注æã¯ã³ã¬ã¯ã·ã§ã³ããããã£ã®ç¶æ¿ã§ãåãã¼ããæ ¼ç´ããChildrenã¯ObservableCollectionã§å®£è¨ãããã¨ãåºæ¥ãªãã®ã§ãå®ç¾©ããéã«èªåã§Listãã©ã¡ãããnewãã¦ä»£å ¥ãã¦ä½¿ç¨ãããã¨ã«ãªãã¾ãã