开发者

WPF Custom Container Class

开发者 https://www.devze.com 2023-01-10 02:49 出处:网络
Well im trying to create a custom container class that contains in my case ViewMappingEntry\'s, so i created a class that inherits Freezable and implements ICollection.. but when creating an instance

Well im trying to create a custom container class that contains in my case ViewMappingEntry's, so i created a class that inherits Freezable and implements ICollection.. but when creating an instance in Blend and browsing it i get this error: Cannot add content to an object of type 'ViewModelMapping'..

Note: It compiles fine and i get no error in Visual studio, so what am i missing?

public class ViewModelMapping : Freezable, ICollection<MappingEntry>
{
    object locker = new object();

    List<MappingEntry> internalList = new List<MappingEntry>();

    #region ICollection<MappingEntry> Members

    public void Add(MappingEntry item)
    {
        if (this.IsFrozen || this.IsSealed)
            throw new InvalidOperationException("ViewModelMapping is frozen");
        lock (locker)
            internalList.Add(item);
    }

    public void Clear()
    {
        if (this.IsFrozen || this.IsSealed)
            throw new InvalidOperationException("ViewModelMapping is frozen");
        lock (locker)
            internalList.Clear();
    }

    public bool Contains(MappingEntry item)
    {
        lock (locker)
            return internalList.Contains(item);
    }

    public void CopyTo(MappingEntry[] array, int arrayIndex)
    {
        lock (locker)
            internalList.CopyTo(array, arrayIndex);
    }

    public int Count
    {
        get 
        {
            lock (locker)
                return internalList.Count;
        }
    }

    public bool IsReadOnly
    {
        get { return base.IsFrozen || base.IsSealed; }
    }

    public bool Remove(MappingEntry item)
    {
        if (this.IsFrozen || this.IsSealed)
            throw new InvalidOperationException("ViewModelMapping is frozen");
        lock (locker)
            return internalList.Remove(item);
    }

    #endregion

    #region IEnumerable<MappingEntry> Members

    public IEnumerator<MappingEntry> GetEnumerator()
    {
        lock (locker)
            return (IEnumerator<M开发者_开发技巧appingEntry>)internalList.ToArray().GetEnumerator();
    }

    #endregion

    #region IEnumerable Members

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        lock (locker)
            return internalList.ToArray().GetEnumerator();
    }

    #endregion

    protected override Freezable CreateInstanceCore()
    {
        ViewModelMapping viewModelMapping = new ViewModelMapping();
        lock(locker)
            viewModelMapping.internalList = new List<MappingEntry>(this.internalList.ToArray());

        return viewModelMapping;
    }
}


after trying around i found a solution if i implemented all these interfaces -> ICollection, IList, IList, ICollection, IEnumerable it started working in blend.

0

精彩评论

暂无评论...
验证码 换一张
取 消