Best way to convert List <Object> to ObservableCollection <Object>

I have a List<Object> where the Object has many child List<Object> around 4-6 levels.

Now I need to bind it to WPF TreeView ... :(

What is the best way to convert it to an ObservableCollection<Object> ??

+12
c # wpf
Jan 30 2018-12-12T00:
source share
6 answers

One part of the answer is to use Reactive Extensions (Rx). You can get it from NuGet and developed by Microsoft. With it, you can simply say: myListCollection.ToObservable();

If your child collections are always in the node with the same name, you can use while(item.Collection != null || item.Collection.Count == 0) and put ToObservable() in a loop

+4
Jan 30 '12 at 19:23
source share

Assuming you mean ObservableCollection<T> , if you want List be directly with ObservableCollection as-is, just use the constructor:

 var oc = new ObservableCollection<Object>(yourListOfObject); 

Now, if you want to untwist each of them, you will need to do some work to collapse them into one ObservableCollection<T> .

+18
Jan 30 '12 at 19:22
source share

Assuming you have a node class defined as follows:

 public class Node { public ICollection<Node> Children { get; set; } } 

You can use recursion to convert List<Node> collections at any depth level:

 public static ObservableCollection<Node> ToObservableRecursive(ICollection<Node> nodes) { foreach (Node node in nodes) if (node.Children != null) node.Children = ToObservableRecursive(node.Children); return new ObservableCollection<Node>(nodes); } 
+3
Jan 30 '12 at 19:31
source share

I assume you are talking about ObservableCollection. To just answer, you can use ctor:

 List<Object> myList = GetList(); new ObservableCollection<Object>(myList); 

However, I think there is some work that needs to be done in terms of organizing your information hierarchically.

+1
Jan 30 '12 at 19:23
source share

Do you mean ObservableCollection ? If you want the Lists child level to be watched as well, you will need to traverse the tree and change the elements as needed, or add each element separately to begin with.

+1
Jan 30 2018-12-12T00:
source share

You cannot implement any of the solutions listed here until your types are the same! For example, if your list or ICollection or IEnumerable is of type string , but your ObservableCollection must be of type Nodes or Computers or something else, you must first get a List of this type! I spent so much time on these other fictitious, simplified "solutions" that are NOT solutions because they do not explain this or do not take this factor into account.

Say this is your ObservableCollection :

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

Pretty standard, right? You are likely to replace Computer with any number of objects in any number of projects.

If you then get a list of computer names in a List<string> called lstComputers , you CANNOT just convert this using:

 var oc = new ObservableCollection<Computer>(lstComputers); 

He will say that your types are incompatible and cannot be converted from one to another, because you are trying to drag the List<string> into the ObservableCollection<Computer> . Square snap in a round hole.

Instead, in your function to get the names of computers, you should add all of them "special":

 public static List<Computer> NetworkComputers() { List<Computer> lstComputers = new List<Computer>(); DirectoryEntry root = new DirectoryEntry("WinNT:"); foreach (DirectoryEntry computers in root.Children) { foreach (DirectoryEntry computer in computers.Children) { if (computer.Name != "Schema" && computer.SchemaClassName == "Computer") { lstComputers.Add(new Computer() { Name = computer.Name }); } } } } 

Just because we used this line lstComputers.Add(new Computer() { Name = computer.Name }); and return List<Computer> , not List<string> , can we now put it in our ObservableCollection<Computer> .

If you missed a boat and cannot do it from the very beginning, this thread talks about other ways you could do: convert a list of objects from one type to another using the lambda expression

So, as soon as you get it in the List<Computer> , only then you can do:

 List<Computer> lstComputers = NetworkComputers(); var oc = new ObservableCollection<Computer>(lstComputers); 
0
Jan 18 '17 at 23:41
source share



All Articles