问题
i have an separate class, let's say class2, with a dictionary. i'd like it to fill the dictionary with values.
public class Class2
{
public Class2()
{
public Dictionary<string, string> someDictionary = new Dictionary<string, string>();
//I DON"T KNOW HOW TO CREATE AN EVENTHANDLER HERE FOR THE DICTIONARY
//how do i create the eventhandler if someDictionary changes?
}
}
let's say in class1, i have a listview that i'd like to fill with the dictionary values, but change them as the dictionary in class2 changes.
public class Class1
{
public Class1()
{
Class2 class2 = new Class2();
class2.SomeDictionaryChanged += new EventHandler(someDictionary_SomeDictionaryChanged);
}
void someData_SomeDataChanged(object sender, EventArgs e)
{
listView1.Add(class2.someDictionary);
}
}
how do i create an eventhandler for a dictionary in class2, and how do i check if the dictionary changed in class1?
ALL OF THIS IS PSEUDOCODE, IT WON'T COMPILE, I DON'T KNOW HOW TO DO THIS, THUS I'M ASKING THE QUESTION.
回答1:
Check out the ObservableDictionary. http://blogs.microsoft.co.il/blogs/shimmy/archive/2010/12/26/observabledictionary-lt-tkey-tvalue-gt-c.aspx
It implements INotifyCollectionChanged and INotifyPropertyChanged. You can handle CollectionChanged from one dictionary to update another.
I haven't tested this code, but it should get you in the right direction.
public class Class2 : ObservableDictionary<string, string>
{
public Class2()
{
}
}
public class Class1
{
public Class1()
{
Class2 class2 = new Class2();
class2.CollectionChanged += new OnCollectionChanged;
}
void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
foreach (KeyValuePair<string, string> kvp in e.NewItems)
{
listView1.Add(kvp.Value);
}
}
}
}
Where class1 extends the observable dictionary, which has the CollectionChanged event. In class1, you can attach to class2's CollectionChanged event and use it to add items to your listview.
回答2:
This is not possible, from the standpoint of only using a dictionary.
Here's why:
Dictionary<TKey, TValue
does not have any events- Comparing two
Dictionary<TKey, TValue>
objects, that have the sameTkey
andTValue
types, and the same content, will compare tofalse
, simply because.Equals(x)
on such a dictionary uses reference comparison - Checking the hashcode through
.GetHashCode()
will not work either because the hash codes of two difference dictionary instances is likely to be different, even if the dictionarys have the same content.
The only way to get something like this to work is by either:
- Subclassing
Dictionary<TKey, TValue>
and firing events on everything that changes the dictionary (note that I don't really remember whether the dictionary type is sealed or not, ie. whether this is actually possible) - Wrapping all access to the underlying dictionary, and firing events at the appropriate places
来源:https://stackoverflow.com/questions/8621276/c-sharp-check-if-a-dictionary-has-changed-values-in-another-class-through-an-eve