64 lines
1.7 KiB
C#
64 lines
1.7 KiB
C#
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Dynamic;
|
|
|
|
namespace JD.Guinness.WPF.Module.Model
|
|
{
|
|
public sealed class CustomData : DynamicObject, INotifyPropertyChanged
|
|
{
|
|
private readonly Dictionary<string, string> _values;
|
|
|
|
public IEnumerable<KeyValuePair<string, string>> Values => _values;
|
|
|
|
public string this[string key]
|
|
{
|
|
get => _values.TryGetValue(key, out string value) ? value : string.Empty;
|
|
set => _values[key] = value;
|
|
}
|
|
|
|
public CustomData()
|
|
{
|
|
_values = new Dictionary<string, string>();
|
|
}
|
|
|
|
public void Add(IEnumerable<KeyValuePair<string, string>> data)
|
|
{
|
|
foreach (KeyValuePair<string, string> pair in data)
|
|
{
|
|
this[pair.Key] = pair.Value;
|
|
}
|
|
}
|
|
|
|
public void Remove(string key) => _values.Remove(key);
|
|
|
|
public void ChangeKey(string oldKey, string newKey)
|
|
{
|
|
if (_values.TryGetValue(oldKey, out string value))
|
|
{
|
|
Remove(oldKey);
|
|
_values[newKey] = value;
|
|
}
|
|
}
|
|
|
|
public override bool TryGetMember(GetMemberBinder binder, out object result)
|
|
{
|
|
result = this[binder.Name];
|
|
|
|
return true;
|
|
}
|
|
|
|
public override bool TrySetMember(SetMemberBinder binder, object value)
|
|
{
|
|
this[binder.Name] = value as string;
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(binder.Name));
|
|
|
|
return true;
|
|
}
|
|
|
|
#region INotifyPropertyChanged
|
|
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
#endregion
|
|
}
|
|
}
|