2013-04-05 90 views
1

是否可以在自定义类中的Key上绑定,如可能与Dictionary一样?用自定义类绑定密钥

我可以绑定到一个Dictionary与此结合:

"Binding MyDictionary[MyKey]" 

但我不希望使用Dictionary但自定义类这样的:

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"); 
     } 
    } 
} 

现在我要访问这个类中的对象在ObservableCollection之内,就像我用Dictionary做的那样。 有没有办法实现这个目标?

回答

4

可以,但你必须实现indexer property

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

编辑:

但是,如果你有兴趣在具有INotifyProperty/CollectionChange执行字典,你可以使用ObservableDictionary

+0

索引器属性正是我所期待的!谢谢! – 2013-04-05 12:48:18