问题描述
我有一个单独的课,让我们说class2,一个字典。我希望它用值填充字典。 public class Class2
{
public Class2 ()
{
public Dictionary< string,string> someDictionary = new Dictionary< string,string>();
// I DONT知道如何在此处创建一个事件处理程序
//如果someDictionary更改,我如何创建事件处理程序?
}
}
让我们在class1中说一个列表视图,我想填写字典值,但是将它们更改为:
class2中的字典更改。
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);
}
}
如何在class2中为字典创建一个事件处理程序,如何检查class1中的字典是否已更改?
所有这些都是PSEUDOCODE,它不会完成,我不会KNO如何做到这一点,这个我在问问题。
查看ObservableDictionary。
它实现INotifyCollectionChanged和INotifyPropertyChanged。
您可以从一个字典处理CollectionChanged以更新另一个字典。
我还没有测试过这个代码,但它应该让你朝着正确的方向。 p>
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);
}
}
}
}
class1扩展了可观察字典,它具有CollectionChanged事件。
在class1中,您可以附加到class2的CollectionChanged事件,并使用它将项目添加到列表视图中。
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.
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.
这篇关于c#检查字典是否通过事件处理程序更改了另一个类中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!