Robert Nagel
Robert Nagel

Reputation: 149

Binding on key withing a custom class

Is it possible to bind on a Key within a custom class like it is possible with a Dictionary?

I can bind to a Dictionary with this binding:

"Binding MyDictionary[MyKey]" 

But I don´t want to use a Dictionary but a custom class like this:

public class DataGridField :  INotifyPropertyChanged
{
    [Required]
    [Key]
    public string Key { get; set; }

    private object value;
    public object Value
    {
        get
        {
            return this.value;
        }
        set
        {
            this.value = value;
            this.OnPropertyChanged("Value");
        }
    }
}

Now I want to access objects of this class within a ObservableCollection the same way I do it with the Dictionary. Is there some way to achieve this?

Upvotes: 1

Views: 90

Answers (1)

Miłosz Wierzbicki
Miłosz Wierzbicki

Reputation: 2112

You can, but you have to implement indexer property:

public class DataGridField
{
    public string this[string index]
    {
        get { return this.Key; }
        set
        {
            this.Key = index;
        }
     }
 }

Edit:

But if you are interested in having Dictionary with INotifyProperty/CollectionChange implementation, you can use ObservableDictionary

Upvotes: 4

Related Questions