Treeview显示多个相同的节点

时间:2014-04-03 03:39:05

标签: c# wpf xaml

我有一个树视图,如果有子视图,则返回多个实例。

这里我用C#WPF .Net 4.5应用程序看看它的样子。

enter image description here

我正在做这样的事情

这是我的实体类

public class Employee
        {
            public Employee()
            {

                this.Employees1 = new HashSet<Employee>();
            }

            public int EmployeeId { get; set; }
            public Nullable<int> ParentId { get; set; }
            public string Employee1 { get; set; }

            public virtual ICollection<Employee> Employees1 { get; set; }
            public virtual Employee Employee2 { get; set; }
        }

查看模型看起来像这样

    private readonly ObservableCollection<Employee> _employees = new ObservableCollection<Employee>();

    public ObservableCollection<Employee> Employees
    {
        get { return _employees; }
    }
    public EmployeeViewModel()
    {
    }

    public EmployeeViewModel(object owner)
    {
        LoadEmployees();
    }

    public void LoadEmployees()
    {
        try
        {
            var list = new ObservableCollection<Employee>
            {
                new Employee {EmployeeId = 1, Employee1 = "Emp 1", ParentId = null},
                new Employee {EmployeeId = 2, Employee1 = "Emp 2", ParentId = 1},
                new Employee {EmployeeId = 3, Employee1 = "Emp 3", ParentId = 2},
                new Employee {EmployeeId = 4, Employee1 = "Emp 4", ParentId = 3},
                new Employee {EmployeeId = 5, Employee1 = "Emp 5", ParentId = null},
                new Employee {EmployeeId = 6, Employee1 = "Emp 6", ParentId = null},
                new Employee {EmployeeId = 7, Employee1 = "Emp 7", ParentId = 6},
                new Employee {EmployeeId = 8, Employee1 = "Emp 8", ParentId = null},
                new Employee {EmployeeId = 9, Employee1 = "Emp 9", ParentId = 8},
                new Employee {EmployeeId = 10, Employee1 = "Emp 10", ParentId = null}
            };
            list.ForEach(t => _employees.Add(t));
            ProcessRootNodes();
        }
        catch (Exception e)
        {

            throw e;
        }
    }

public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        private void ProcessRootNodes()
        {
            var rootNodes = _employees.Where(x => x.ParentId == null).ToList();

            foreach (Employee t in rootNodes)
            {
                t.Employees1 = this.AddChildren(t);
            }
        }

        private List<Employee> AddChildren(Employee entry)
        {
            var children = _employees.Where(x => x.ParentId == entry.EmployeeId).ToList();

            foreach (Employee t in children)
            {
                t.Employees1 = this.AddChildren(t);
            }
            return children;
        }
像这样的

xaml.cs文件

public MainWindow()
        {
            InitializeComponent(); Loaded += OnLoaded;
        }

        public void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
        {
            DataContext = new EmployeeViewModel("Object");
        }

Xaml看起来像这样

<UserControl.Resources>
    <HierarchicalDataTemplate x:Key="NodeTemplate"
    ItemsSource="{Binding Employees1}">
        <TextBlock Text="{Binding Employee1}"/>
    </HierarchicalDataTemplate>
</UserControl.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="325" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <telerik:RadTreeView x:Name="radTreeView" Margin="8"
    ItemsSource="{Binding Employees}"
    ItemTemplate="{StaticResource NodeTemplate}"/>        
</Grid>

1 个答案:

答案 0 :(得分:1)

将此类文件用于显示树,并在xml文件中添加节点

   using System;
    using System.Xml;
    using System.Windows.Forms;
    using System.Text;


namespace Project_Management
{
    /// <summary>
    /// Summary description for TreeViewSerializer.
    /// </summary>
    public class TreeViewSerializer
    {

        // Xml tag for node, e.g. 'node' in case of <node></node>
        private const string XmlNodeTag = "node";

        // Xml attributes for node e.g. <node text="Asia" tag="" imageindex="1"></node>
        private const string XmlNodeTextAtt = "text";
        private const string XmlNodeTagAtt = "tag";
        private const string XmlNodeImageIndexAtt = "imageindex";

        public TreeViewSerializer()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        //System.IO.StringWriter s;
        public void SerializeTreeView(TreeView treeView, string fileName)
        {
            XmlTextWriter textWriter = new XmlTextWriter(fileName, System.Text.Encoding.ASCII);
            // writing the xml declaration tag
            textWriter.WriteStartDocument();
            //textWriter.WriteRaw("\r\n");
            // writing the main tag that encloses all node tags
            textWriter.WriteStartElement("TreeView");

            // save the nodes, recursive method
            SaveNodes(treeView.Nodes, textWriter);

            textWriter.WriteEndElement();

            textWriter.Close();
        }

        private void SaveNodes(TreeNodeCollection nodesCollection,
            XmlTextWriter textWriter)
        {
            for (int i = 0; i < nodesCollection.Count; i++)
            {
                TreeNode node = nodesCollection[i];
                textWriter.WriteStartElement(XmlNodeTag);
                textWriter.WriteAttributeString(XmlNodeTextAtt, node.Text);
                textWriter.WriteAttributeString(XmlNodeImageIndexAtt, node.ImageIndex.ToString());
                if (node.Tag != null)
                    textWriter.WriteAttributeString(XmlNodeTagAtt, node.Tag.ToString());

                // add other node properties to serialize here

                if (node.Nodes.Count > 0)
                {

                    SaveNodes(node.Nodes, textWriter);

                }
                textWriter.WriteEndElement();
            }
        }

        public void DeserializeTreeView(TreeView treeView, string fileName)
        {
            XmlTextReader reader = null;
            try
            {
                // disabling re-drawing of treeview till all nodes are added
                treeView.BeginUpdate();
                reader =
                    new XmlTextReader(fileName);

                TreeNode parentNode = null;

                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        if (reader.Name == XmlNodeTag)
                        {
                            TreeNode newNode = new TreeNode();
                            bool isEmptyElement = reader.IsEmptyElement;

                            // loading node attributes
                            int attributeCount = reader.AttributeCount;
                            if (attributeCount > 0)
                            {
                                for (int i = 0; i < attributeCount; i++)
                                {
                                    reader.MoveToAttribute(i);

                                    SetAttributeValue(newNode, reader.Name, reader.Value);
                                }
                            }

                            // add new node to Parent Node or TreeView
                            if (parentNode != null)
                                parentNode.Nodes.Add(newNode);
                            else
                                treeView.Nodes.Add(newNode);

                            // making current node 'ParentNode' if its not empty
                            if (!isEmptyElement)
                            {
                                parentNode = newNode;
                            }

                        }
                    }
                    // moving up to in TreeView if end tag is encountered
                    else if (reader.NodeType == XmlNodeType.EndElement)
                    {
                        if (reader.Name == XmlNodeTag)
                        {
                            parentNode = parentNode.Parent;
                        }
                    }
                    else if (reader.NodeType == XmlNodeType.XmlDeclaration)
                    { //Ignore Xml Declaration                    
                    }
                    else if (reader.NodeType == XmlNodeType.None)
                    {
                        return;
                    }
                    else if (reader.NodeType == XmlNodeType.Text)
                    {
                        parentNode.Nodes.Add(reader.Value);
                    }

                }
            }
            finally
            {
                // enabling redrawing of treeview after all nodes are added
                treeView.EndUpdate();
                reader.Close();
            }
        }

        /// <summary>
        /// Used by Deserialize method for setting properties of TreeNode from xml node attributes
        /// </summary>
        /// <param name="node"></param>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        private void SetAttributeValue(TreeNode node, string propertyName, string value)
        {
            if (propertyName == XmlNodeTextAtt)
            {
                node.Text = value;
            }
            else if (propertyName == XmlNodeImageIndexAtt)
            {
                node.ImageIndex = int.Parse(value);
            }
            else if (propertyName == XmlNodeTagAtt)
            {
                node.Tag = value;
            }
        }

        public void LoadXmlFileInTreeView(TreeView treeView, string fileName)
        {
            XmlTextReader reader = null;
            try
            {
                treeView.BeginUpdate();
                reader = new XmlTextReader(fileName);

                TreeNode n = new TreeNode(fileName);
                treeView.Nodes.Add(n);
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        bool isEmptyElement = reader.IsEmptyElement;
                        StringBuilder text = new StringBuilder();
                        text.Append(reader.Name);
                        int attributeCount = reader.AttributeCount;
                        if (attributeCount > 0)
                        {
                            text.Append(" ( ");
                            for (int i = 0; i < attributeCount; i++)
                            {
                                if (i != 0) text.Append(", ");
                                reader.MoveToAttribute(i);
                                text.Append(reader.Name);
                                text.Append(" = ");
                                text.Append(reader.Value);
                            }
                            text.Append(" ) ");
                        }

                        if (isEmptyElement)
                        {
                            n.Nodes.Add(text.ToString());
                        }
                        else
                        {
                            n = n.Nodes.Add(text.ToString());
                        }
                    }
                    else if (reader.NodeType == XmlNodeType.EndElement)
                    {
                        n = n.Parent;
                    }
                    else if (reader.NodeType == XmlNodeType.XmlDeclaration)
                    {

                    }
                    else if (reader.NodeType == XmlNodeType.None)
                    {
                        return;
                    }
                    else if (reader.NodeType == XmlNodeType.Text)
                    {
                        n.Nodes.Add(reader.Value);
                    }

                }
            }
            finally
            {
                treeView.EndUpdate();
                reader.Close();
            }
        }
    }
}