안녕하세요.
제가 현재 트리 구조를 제작 중입니다.
(참고로 Caliburn.Micro를 이용중이라, NotifyOfPropertyChange라는 갱신 메소드를 통해 화면 갱신합니다.)
Recursive하게 Linq를 이용해서 로직을 구현하고 싶은데요.
현재 트리구조는 아래와 같습니다.
TreeModel.cs
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public EnumTreeType Type { get; set; }
public bool Used { get; set; }
public bool Visibility { get; set; }
public object ParentTree { get; set; }
TreeItemViewModel.cs
public int Id
{
get => Model.Id;
set
{
Model.Id = value;
NotifyOfPropertyChange(() => Id);
}
}
public string Name
{
get => Model.Name;
set
{
Model.Name = value;
NotifyOfPropertyChange(() => Name);
}
}
/// <summary>
/// 트리 노드의 해설 -
/// 트리 노드를 묘사하는 문구를 입력
/// </summary>
public string Description
{
get => Model.Description;
set
{
Model.Description = value;
NotifyOfPropertyChange(() => Description);
}
}
/// <summary>
/// 트리 노드의 타입 -
/// 트리 노드의 타입을 지정할 수 있다.
/// 1.ROOT : 부모가 없고, 자식만 존재 최초 노드
/// 2.BRANCH : 부모와 자식이 둘다 존재 중간 노드
/// 3.LEAF : 자식은 없고 부모만 존재 말단 노드
/// </summary>
public EnumTreeType Type
{
get => Model.Type;
set
{
Model.Type = value;
NotifyOfPropertyChange(() => Type);
}
}
public bool Used
{
get => Model.Used;
set
{
Model.Used = value;
NotifyOfPropertyChange(() => Used);
}
}
public bool Visibility
{
get => Model.Visibility;
set
{
Model.Visibility = value;
NotifyOfPropertyChange(() => Visibility);
}
}
public object ParentTree
{
get => Model.ParentTree;
set
{
Model.ParentTree = value;
NotifyOfPropertyChange(() => ParentTree);
}
}
{
get { return _children; }
set
{
_children = value;
NotifyOfPropertyChange(() => Children);
}
}
private ObservableCollection<TreeItemViewModel> _children;
ViewModel을 통해서 로직을 만들고 핼퍼클래스로 재귀적으로 갯수 계산하는 로직 구현하려고하는데요.
제가 짠 헬퍼 클래스의 재귀메소드는 이렇습니다.
조언을 구합니다.
public static class TreeManager
{
public static int Count;
public static int GetTreeCount(ObservableCollection<TreeItemViewModel> collection)
{
/// Get The number of node with the recursive travers of tree structure
/// node0
/// ㄴnode1
/// ㄴnode4
/// ㄴnode7
/// ㄴnode2
/// ㄴnode5
/// ㄴnode3
/// ㄴnode6
///
Count = 0;
TreeRecursizeCounter(collection);
return Count;
}
private static void TreeRecursizeCounter(ObservableCollection<TreeItemViewModel> collection)
{
if (!(collection != null && collection.Count() > 0))
{
Debug.WriteLine($"자손없음");
return;
}
foreach (var item in collection)
{
Debug.WriteLine($"{item.DisplayName} 도착!! 총 노드 : {++Count}개");
if (item.Children.Count() > 0)
{
Debug.WriteLine($"하위 레벨 진입");
TreeRecursizeCounter(item.Children);
}
}
}
}
이정도 입니다.
감사합니다.