Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

BindableDynamicDictionary

/// <summary>
/// Bindable dynamic dictionary.
/// </summary>
public sealed class BindableDynamicDictionary : DynamicObject, INotifyPropertyChanged
{
    /// <summary>
    /// The internal dictionary.
    /// </summary>
    private readonly Dictionary<string, object> _dictionary;

    /// <summary>
    /// Creates a new BindableDynamicDictionary with an empty internal dictionary.
    /// </summary>
    public BindableDynamicDictionary()
    {
        _dictionary = new Dictionary<string, object>();
    }

    /// <summary>
    /// Copies the contents of the given dictionary to initilize the internal dictionary.
    /// </summary>
    /// <param name="source"></param>
    public BindableDynamicDictionary(IDictionary<string, object> source)
    {
        _dictionary = new Dictionary<string, object>(source);
    }
    /// <summary>
    /// You can still use this as a dictionary.
    /// </summary>
    /// <param name="key"></param>
    /// <returns></returns>
    public object this[string key]
    {
        get
        {
            return _dictionary[key];
        }
        set
        {
            _dictionary[key] = value;
            RaisePropertyChanged(key);
        }
    }

    /// <summary>
    /// This allows you to get properties dynamically.
    /// </summary>
    /// <param name="binder"></param>
    /// <param name="result"></param>
    /// <returns></returns>
    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        return _dictionary.TryGetValue(binder.Name, out result);
    }

    /// <summary>
    /// This allows you to set properties dynamically.
    /// </summary>
    /// <param name="binder"></param>
    /// <param name="value"></param>
    /// <returns></returns>
    public override bool TrySetMember(SetMemberBinder binder, object value)
    {
        _dictionary[binder.Name] = value;
        RaisePropertyChanged(binder.Name);
        return true;
    }

    /// <summary>
    /// This is used to list the current dynamic members.
    /// </summary>
    /// <returns></returns>
    public override IEnumerable<string> GetDynamicMemberNames()
    {
        return _dictionary.Keys;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string propertyName)
    {
        var propChange = PropertyChanged;
        if (propChange == null) return;
        propChange(this, new PropertyChangedEventArgs(propertyName));
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: .net core executenonqueryasync transaction 
Csharp :: calculated field gridview asp.net 
Csharp :: reflection static method c# 
Csharp :: c# convert linq jValue to int 
Csharp :: c# linq unique by property 
Csharp :: .net mvc foreach with index 
Csharp :: edit pdf itextsharip 
Csharp :: c# open explorer and select file 
Csharp :: how to make a 2d character move in unity 2020 
Csharp :: c# sort a list of objects 
Csharp :: wpf user parent controller datacontext 
Csharp :: parsing object from text file c# 
Csharp :: c# unzip all archive files inside directory 
Csharp :: remove language folders build visual studio 
Csharp :: unity oculus vibrate 
Csharp :: translate english to spanish 
Csharp :: c# string .contains against empty string returns 
Csharp :: how to set an expiry date on a program 
Csharp :: get user by username c# 
Csharp :: leantween unity when timescale 0 
Csharp :: how to pass object as test case in nunit c# 
Csharp :: how to get the screen size in Tao.Freeglut 
Csharp :: membership get user id 
Csharp :: ddsfsd 
Csharp :: ExceptionFilterAttribute exception-handler-middleware-not-catching 
Csharp :: leave two decimal in double c# 
Csharp :: death transition unity 2d 
Csharp :: bitter foods examplews 
Csharp :: c# webbrowser control append 
Csharp :: static {} 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =