none
wpf中一个数据处理的问题 RRS feed

  • 问题

  • D:\123\1\2k

    D:\123\2\en-us

    D:\345\en\vista64

    D:\345\_en\xp

    D:\678\zh\xp64

    上面是数据。 怎么样能把这些数据形成一个树形

    即 d:

    123

                   1

    2

    345

    678 (其余.....)

    前台:    <TreeView Name="treeview"></TreeView>

    2011年5月11日 9:07

答案

  • 这是我现在的想法

    按照这条数据的路径,按顺序转成tree,但是 不是我心里要的,现在不知道怎么改,麻烦各位了。 

    string tempchar = @"D:\123\1\2k*D:\123\2\en-us*D:\345\en\vista64*D:\345\_en\xp*D:\678\zh\xp64";
                string[] tempchars = tempchar.Split(new string[]{"*"},StringSplitOptions.RemoveEmptyEntries);
                System.Windows.Controls.TreeViewItem treestr;
                foreach (string str in tempchars)
                {
                    string[] treeitemStr = str.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries);

                    treestr = new System.Windows.Controls.TreeViewItem();
                    treestr.IsExpanded = true;
                    AddTreeItem(treestr, treeitemStr,0);
                    treeview.Items.Add(treestr);
                }

    /// <summary>
            /// 按照一个路径生成树形
            /// </summary>
            /// <param name="treestr"></param>
            /// <param name="pChild"></param>
            /// <param name="p"></param>
            private void AddTreeItem(TreeViewItem treestr, string[] pChild, int p)
            {
                System.Windows.Controls.TreeViewItem childTreeItem = new System.Windows.Controls.TreeViewItem();
                childTreeItem.Header = pChild[p];
                childTreeItem.IsExpanded = true;
                StringBuilder sbuild = new StringBuilder();
                for (int i = 0; i <= p; i++)
                {
                    sbuild.Append(pChild[i]);
                    sbuild.Append("\\");
                }
                childTreeItem.Tag = sbuild.ToString();
                string name = "p" + p.ToString();
                childTreeItem.Name = name;
                treestr.Items.Add(childTreeItem);
                p++;
                if (pChild.Length <= p) return;
                AddTreeItem(childTreeItem, pChild, p);
            }

     

    • 已标记为答案 wpf001 2011年5月12日 0:52
    2011年5月11日 9:18
  • 没按着你的写。但是这个结构你理解一下。我是用绑定

     

     public partial class topics
        {
            private ObservableCollection<topics> childTopics = new ObservableCollection<topics>();

            public ObservableCollection<topics> ChildTopics
            {
                get { return childTopics; }
                set { childTopics = value; }
            }
            public topics(string Title, int Rating)
            {
                this.Title = Title;
                this.Rating = Rating;
            }
        }
        //public class Topic
        //{
        //    public string Title { get; set; }
        //    public int Rating { get; set; }
        //    private ObservableCollection<Topic> childTopicsValue = new ObservableCollection<Topic>();
        //    public ObservableCollection<Topic> ChildTopics
        //    {
        //        get
        //        {
        //            return childTopicsValue;
        //        }
        //        set
        //        {
        //            childTopicsValue = value;
        //        }
        //    }
        //    public Topic() { }
        //    public Topic(string title, int rating)
        //    {
        //        Title = title;
        //        Rating = rating;
        //    }
        //}

     

     

     

    //Topics.Add(new Topic("Using Controls and Dialog Boxes", -1));
                //Topics.Add(new Topic("Getting Started with Controls", 1));
                //Topic DataGridTopic = new Topic("DataGrid", 4);
                //DataGridTopic.ChildTopics.Add(
                //    new Topic("Default Keyboard and Mouse Behavior in the DataGrid Control", -1));
                //DataGridTopic.ChildTopics.Add(
                //    new Topic("How to: Add a DataGrid Control to a Page", -1));
                //DataGridTopic.ChildTopics.Add(
                //    new Topic("How to: Display and Configure Row Details in the DataGrid Control", 1));
                //Topics.Add(DataGridTopic);
                //this.treeView1.DataContext = Topics;
                this.treeView1.DataContext = dc.topics;

    • 已标记为答案 wpf001 2011年5月12日 0:52
    2011年5月11日 9:48

全部回复

  • 这是我现在的想法

    按照这条数据的路径,按顺序转成tree,但是 不是我心里要的,现在不知道怎么改,麻烦各位了。 

    string tempchar = @"D:\123\1\2k*D:\123\2\en-us*D:\345\en\vista64*D:\345\_en\xp*D:\678\zh\xp64";
                string[] tempchars = tempchar.Split(new string[]{"*"},StringSplitOptions.RemoveEmptyEntries);
                System.Windows.Controls.TreeViewItem treestr;
                foreach (string str in tempchars)
                {
                    string[] treeitemStr = str.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries);

                    treestr = new System.Windows.Controls.TreeViewItem();
                    treestr.IsExpanded = true;
                    AddTreeItem(treestr, treeitemStr,0);
                    treeview.Items.Add(treestr);
                }

    /// <summary>
            /// 按照一个路径生成树形
            /// </summary>
            /// <param name="treestr"></param>
            /// <param name="pChild"></param>
            /// <param name="p"></param>
            private void AddTreeItem(TreeViewItem treestr, string[] pChild, int p)
            {
                System.Windows.Controls.TreeViewItem childTreeItem = new System.Windows.Controls.TreeViewItem();
                childTreeItem.Header = pChild[p];
                childTreeItem.IsExpanded = true;
                StringBuilder sbuild = new StringBuilder();
                for (int i = 0; i <= p; i++)
                {
                    sbuild.Append(pChild[i]);
                    sbuild.Append("\\");
                }
                childTreeItem.Tag = sbuild.ToString();
                string name = "p" + p.ToString();
                childTreeItem.Name = name;
                treestr.Items.Add(childTreeItem);
                p++;
                if (pChild.Length <= p) return;
                AddTreeItem(childTreeItem, pChild, p);
            }

     

    • 已标记为答案 wpf001 2011年5月12日 0:52
    2011年5月11日 9:18
  • 没按着你的写。但是这个结构你理解一下。我是用绑定

     

     public partial class topics
        {
            private ObservableCollection<topics> childTopics = new ObservableCollection<topics>();

            public ObservableCollection<topics> ChildTopics
            {
                get { return childTopics; }
                set { childTopics = value; }
            }
            public topics(string Title, int Rating)
            {
                this.Title = Title;
                this.Rating = Rating;
            }
        }
        //public class Topic
        //{
        //    public string Title { get; set; }
        //    public int Rating { get; set; }
        //    private ObservableCollection<Topic> childTopicsValue = new ObservableCollection<Topic>();
        //    public ObservableCollection<Topic> ChildTopics
        //    {
        //        get
        //        {
        //            return childTopicsValue;
        //        }
        //        set
        //        {
        //            childTopicsValue = value;
        //        }
        //    }
        //    public Topic() { }
        //    public Topic(string title, int rating)
        //    {
        //        Title = title;
        //        Rating = rating;
        //    }
        //}

     

     

     

    //Topics.Add(new Topic("Using Controls and Dialog Boxes", -1));
                //Topics.Add(new Topic("Getting Started with Controls", 1));
                //Topic DataGridTopic = new Topic("DataGrid", 4);
                //DataGridTopic.ChildTopics.Add(
                //    new Topic("Default Keyboard and Mouse Behavior in the DataGrid Control", -1));
                //DataGridTopic.ChildTopics.Add(
                //    new Topic("How to: Add a DataGrid Control to a Page", -1));
                //DataGridTopic.ChildTopics.Add(
                //    new Topic("How to: Display and Configure Row Details in the DataGrid Control", 1));
                //Topics.Add(DataGridTopic);
                //this.treeView1.DataContext = Topics;
                this.treeView1.DataContext = dc.topics;

    • 已标记为答案 wpf001 2011年5月12日 0:52
    2011年5月11日 9:48