Last active
October 29, 2015 22:17
-
-
Save jakesays-old/b2424e4bc4eb799d0378 to your computer and use it in GitHub Desktop.
Simple dictionary binder for wpf.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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