Skip to content

Instantly share code, notes, and snippets.

@jakesays-old
Last active October 29, 2015 22:17
Show Gist options
  • Select an option

  • Save jakesays-old/b2424e4bc4eb799d0378 to your computer and use it in GitHub Desktop.

Select an option

Save jakesays-old/b2424e4bc4eb799d0378 to your computer and use it in GitHub Desktop.
Simple dictionary binder for wpf.
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace Std
{
public class DictionaryBinder : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private readonly Dictionary<string, string> _dictionary;
public DictionaryBinder(string key, Dictionary<string, string> dictionary)
{
Key = key;
_dictionary = dictionary;
}
public string Key { get; }
public string Value
{
get
{
string value;
if (!_dictionary.TryGetValue(Key, out value))
{
return null;
}
return value;
}
set
{
if (_value == value)
{
return;
}
if (_dictionary.ContainsKey(Key))
{
_dictionary.Add(Key, value);
}
else
{
_dictionary[Key] = value;
}
OnPropertyChanged();
}
}
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment