سؤال

How to bind a Tree data structure using hierarchical data template to a tree list control. The difference is that instead of creating different types for each level in hierarchy I intend to use only one type “Node” differentiated by the “Type” enumeration indicating its level in the hierarchy. Is this a feasible approach. How to display the data using TreeListControl.

public class TreeNode<T> where T : new()
    {
        public TreeNode<T> Parent { get; set; }

        public IList<TreeNode<T>> Children { get; set; }

        protected TreeNodeType Type { get;  set; }

        public T Current { get; set; }

        public TreeNode()
        {

        }

        public TreeNode(TreeNodeType type)
        {
            this.Type = type;
            this.Current = new T();
            this.Children = new List<TreeNode<T>>();
        }

        public void AddChildren(TreeNode<T> child)
        {
            child.Parent = this;
            this.Children.Add(child);
        }

        public override string ToString()
        {
            return string.Format("Type :{0} Name :{1}", this.Type, this.Current);
        }
    }



/// <summary>
/// Tree node type
/// </summary>
public enum TreeNodeType
{
    Manager = 0,

    Employee,
}



public class EmployeeNode
{
        public string Name { get; set; }

        public override string ToString()
        {
            return this.Name;
        }
    }
هل كانت مفيدة؟

المحلول 2

I created a data selector that always returns the same template. This will return the same template for each node of the self referencing data.

 public class HierarchialDataTemplateSelector : DataTemplateSelector
    {
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            FrameworkElement element = container as FrameworkElement;

            if (element != null && item != null)
            {
                return element.FindResource("HierarchialDataTemplate") as DataTemplate;
            }
            return null;
        }
    }

نصائح أخرى

You're going to need to use a DataTemplateSelector. MSDN describes how to use one. Even though the example is for a ListBox, you can do this with a TreeView.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top