【问题标题】:Binding on key withing a custom class绑定自定义类中的键
【发布时间】:2013-03-27 16:58:27
【问题描述】:

是否可以像绑定Dictionary 一样在自定义类中绑定Key

我可以使用此绑定绑定到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 一样。 有什么方法可以实现吗?

【问题讨论】:

    标签: c# wpf mvvm binding dictionary


    【解决方案1】:

    可以,但必须实现indexer property

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

    编辑:

    但如果您对使用 INotifyProperty/CollectionChange 实现 Dictionary 感兴趣,您可以使用 ObservableDictionary

    【讨论】:

    • indexer 属性正是我想要的!谢谢!
    猜你喜欢
    • 1970-01-01
    • 2019-11-16
    • 2012-03-10
    • 1970-01-01
    • 2019-10-28
    • 2011-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多