I typed this out freehand so forgive me for any typos.. felt like making a simple class like this would be the easiest way to represent what i'm trying to accomplish in my actual project... anyway

How can I make it so that if i edit an item in one collection/list (Collection_A).. that it'll also reflect the changes simultaneously in an collection/list in a different class (Collection_B)?

Right now the way I would do this is to just iterate Collection_B for the same name and breed.. and if so, replace it to what's in Collection_A...

I feel like there's probably a more elegant solution to this and i'm completely missing something easy.. either that and i'm retarded and my solution is probably the best solution.

I'm just trying to "up" my efficiency and programming techniques.. thanks SO

public class Dog
{
    public string name;
    public string breed;
}

public class DogList
{
    public string listName;
    public static ObservableCollection<Dog> Collection_B;
}

public static ObservableCollection<Dog> Collection_A;


Collection_A.Add(new Dog("Coco", "Chihuahua"));
Collection_A.Add(new Dog("Bubba", "Bulldog"));

DogList.Collection_B.Add(Collection_A[0]);
DogList.Collection_B.Add(Collection_B[0]);


Collection_A[0] = new Dog("Coco Moco", "Chuhuahua")  //how can i get this to reflect in Collection_B simultaneously?
有帮助吗?

解决方案

If the objects in both collections are reference types (e.g., a class rather than a struct), and the same instance is added to both collections, the collections then have a reference to the same object instance. A change made in either place will automatically be reflected in the other.

If, however, you replace the object instance in one collection with another object instance, the other collection will not know about it: it will still hold a reference to the original object. To get around that, you'll want to use the observer pattern.

Depending on the semantics of your collection and your requirements, you could

其他提示

you can use the same object in the two collection (as it is a ref type). so you'r code should look like that :

var typeofdog = new Dog("Coco", "Chihuahua");
Collection_A.Add(typeofdog );
Collection_B.Add(typeofdog );

now you can change Collection_A["YOUR ITEM"] to other value and each collection will still referance to the correct answer: example :

Collection_A["YOUR ITEM"].changeContant();//will reflect on both collections

notice that if you will use the new word this will not work because a new object will be initialized.

Subscribe to CollectionChanged event of the Collection_A and update Collection_B appropriately for each possible NotifyCollectionChangedAction.

Here's the example code:

ObservableCollection<int> cA = new ObservableCollection<int>();
ObservableCollection<int> cB = new ObservableCollection<int>();

cA.CollectionChanged += (sender, ev) =>
{
    switch (ev.Action)
    {
        case NotifyCollectionChangedAction.Add:
            foreach (int i in ev.NewItems)
            {
                Console.WriteLine("Item added");
                cB.Add(i);
            }
            break;
        case NotifyCollectionChangedAction.Replace:
            var idx = ev.NewStartingIndex;
            var value = (int)ev.NewItems[0];
            Console.WriteLine("Item {0} replaced with {1}", idx, value);
            cB[idx] = value;
            break;
    }
};

cA.Add(1);
cA.Add(2);
cA[1] = 3;

Console.WriteLine(cB[1]); // outputs 3

You could simply use the following in your case:

Collection_A[0].name = "Coco Moco";
Collection_A[0].breed = "Chuhuahua";

The changes will be reflected in both lists.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top