用自定义类绑定密钥
问题描述:
是否可以在自定义类中的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
做的那样。 有没有办法实现这个目标?
答
可以,但你必须实现indexer property:
public class DataGridField
{
public string this[string index]
{
get { return this.Key; }
set
{
this.Key = index;
}
}
}
编辑:
但是,如果你有兴趣在具有INotifyProperty/CollectionChange执行字典,你可以使用ObservableDictionary
索引器属性正是我所期待的!谢谢! – 2013-04-05 12:48:18