[英]C# Add event to value inside Dictionary<TKey, TValue>
I'm trying to add an event to the value of a dictionary.我正在尝试将事件添加到字典的值中。 I made my own ObservableDictionary with an event on the Add button, but now I'm trying to do the same for the value.
我用 Add 按钮上的一个事件制作了自己的 ObservableDictionary,但现在我正试图对值做同样的事情。 The dictionary looks like
Dictionary<string, ObservableCollection<string>>
.字典看起来像
Dictionary<string, ObservableCollection<string>>
。
The calling function:调用 function:
public void AddExtension(string key, string value)
{
if (_settings.ExtensionsPerFolder.ContainsKey(key))
{
_settings.ExtensionsPerFolder[key].Add(value);
var values = _settings.ExtensionsPerFolder[key];
values.Add(value);
_settings.ExtensionsPerFolder[key] = values;
}
else
_settings.ExtensionsPerFolder.Add(key, new ObservableCollection<string> { value });
}
My own dictionary implementation:我自己的字典实现:
public class ObservableDictionary<TKey, TValue> : Dictionary<TKey, TValue>
{
public event EventHandler CollectionChanged;
public new TValue this[TKey key]
{
get => base[key];
set
{
base[key] = value;
OnCollectionChanged();
}
}
public new void Add(TKey key, TValue value)
{
base.Add(key, value);
OnCollectionChanged();
}
protected void OnCollectionChanged() => CollectionChanged?.Invoke(this, EventArgs.Empty);
}
The _settings.ExtensionsPerFolder[key].Add(value)
doesn't trigger the OnCollectionChanged
event, but the _settings.ExtensionsPerFolder[key] = values
does. _settings.ExtensionsPerFolder[key].Add(value)
不会触发OnCollectionChanged
事件,但_settings.ExtensionsPerFolder[key] = values
会。
[EDIT] The file in which the Dictionary is initialized: [编辑] 字典初始化的文件:
public class Settings
{
[JsonIgnore]
public string SettingsPath { get; } = "Settings.json";
public ObservableCollection<string> Directories { get; set; } = new ObservableCollection<string>();
public ObservableDictionary<string, ObservableCollection<string>> ExtensionsPerFolder { get; set; } = new ObservableDictionary<string, ObservableCollection<string>>();
public Settings() { }
public void CollectionChanged()
{
Directories.CollectionChanged += CollectionChanged;
ExtensionsPerFolder.CollectionChanged += CollectionChanged;
}
private void CollectionChanged(object sender, EventArgs e)
{
try
{
using StreamWriter writer = new StreamWriter(SettingsPath);
writer.WriteLine(this.ToJson());
}
catch (Exception ex)
{
Logger log = new Logger();
log.Log($"Unable to save: {ex.ToString()}");
}
}
}
Is there any way that I can achieve this power of event control, or do I have to fiddle with the Add functionality within my own Dictionary for it to work.有什么方法可以实现这种事件控制功能,还是我必须在我自己的 Dictionary 中使用 Add 功能才能使其工作。
Thanks in advance.提前致谢。
You have a collection of collections and you've only ever subscribed to observe the outer collection.你有一个 collections 的集合,而你只订阅了观察外部集合。 You've subscribed to
ExtensionsPerFolder.CollectionChanged
which will tell you when you do something like ExtensionsPerFolder[key] = values
, but you haven't subscribed to each inner collection.您已订阅
ExtensionsPerFolder.CollectionChanged
,它会在您执行ExtensionsPerFolder[key] = values
之类的操作时告诉您,但您尚未订阅每个内部集合。 In this case values
should be an ObservableCollection<string>
.在这种情况下,
values
应该是ObservableCollection<string>
。
Each time you create a new ObservableCollection<string>
, you'll want to subscribe to it.每次创建新的
ObservableCollection<string>
时,您都需要订阅它。
Here's where you created a new one:这是您创建新的地方:
_settings.ExtensionsPerFolder.Add(key, new ObservableCollection<string> { value });
You'll also want to add a CollectionChanged
handler for this new collection at this point in your code.此时,您还需要在代码中为这个新集合添加一个
CollectionChanged
处理程序。 That way a statement like _settings.ExtensionsPerFolder[key].Add(value)
will trigger a CollectionChanged
event on that particular collection.这样,像
_settings.ExtensionsPerFolder[key].Add(value)
这样的语句将在该特定集合上触发CollectionChanged
事件。
I'll leave it to you to decide what you want that event handler to do.我将由您决定您希望该事件处理程序做什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.