WPF: TreeView from KeyValuePair

2019-08-28 08:22发布

问题:

I have a WPF application with treeview control. When I implement this method to parse the strings I can find a treeview. Now I want to assign id to each path for example "NetworkControl.AlternateIndexText.Value", will have its own id and "NetworkControl.AddressData.MessageOriginatorID.Value", will have another id associated to it.

Here I have a list of KeyValuePair. What I wanted to do is to parse the strings and create a tree node with associated Id.

      public List<MessageElement> GetRequestTreeNodes()
      {
        var nodes = new List<KeyValuePair<int, string>>();

        nodes.Add(new KeyValuePair<int, string>(1, "NetworkControl.AlternateIndexText.Value"));
        nodes.Add(new KeyValuePair<int, string>(1, "NetworkControl.AddressData.DestinationID"));
        nodes.Add(new KeyValuePair<int, string>(1, "NetworkControl.AddressData.MessageOriginatorID.Value"));

     }

Here is where i am parsing the string and Id. I can create the nodes with name but how can i pass the ids so that I can identify each path

     public List<MessageElement> BuildTree(List<KeyValuePair<int, string>>   strings)
    {
        return (
          from s in strings
          let split = s.Value.Split('.')
          group s by s.Value.Split('.')[0] into g

          select new MessageElement
          {
              ID = ?
              Name = g.Key,
              Children = BuildTree(
                from s in g
                where s.Value.Length > g.Key.Length + 1
                select s.Value.Substring(g.Key.Length + 1))
          }
          ).ToList();

      }


        public List<MessageElement> GetRequestTreeNodes()
      {
        var nodes = new List<KeyValuePair<int, string>>();

        nodes.Add(new KeyValuePair<int, string>(1,  "NetworkControl.AlternateIndexText.Value"));
        nodes.Add(new KeyValuePair<int, string>(1, "NetworkControl.AddressData.DestinationID"));
        nodes.Add(new KeyValuePair<int, string>(1, "NetworkControl.AddressData.MessageOriginatorID.Value"));

     }

can someone help me please? i don't want to make any excuse but I am new both to LINQ and WPF thank you all for your help

标签: c# wpf mvvm