Wpf ICollectionView Binding item cannot resolve property of type object

前端 未结 3 1420
孤城傲影
孤城傲影 2021-02-19 15:00

I have bound a GridView with an ICollectionView in the XAML designer the properties are not known because the entity in the CollectionView

3条回答
  •  时光说笑
    2021-02-19 15:41

    Your entity has not been transformed in object, it's because the interface ICollectionView is not a generic collection so ReSharper has no way to know that it holds a collection of Person.

    You can create a generic version of ICollectionView and use it for your PersonCollection property as demonstrated in this post https://benoitpatra.com/2014/10/12/a-generic-version-of-icollectionview-used-in-a-mvvm-searchable-list/.

    First an interface:

    public interface ICollectionView : IEnumerable, ICollectionView
    {
    }
    

    The implementation:

    public class GenericCollectionView : ICollectionView, IEditableCollectionView
    {
        #region Fields
    
        [NotNull] readonly ListCollectionView collectionView;
    
        #endregion
    
        #region Properties
    
        public CultureInfo Culture
        {
            get => collectionView.Culture;
            set => collectionView.Culture = value;
        }
    
        public IEnumerable SourceCollection => collectionView.SourceCollection;
    
        public Predicate Filter
        {
            get => collectionView.Filter;
            set => collectionView.Filter = value;
        }
    
        public bool CanFilter => collectionView.CanFilter;
    
        public SortDescriptionCollection SortDescriptions => collectionView.SortDescriptions;
    
        public bool CanSort => collectionView.CanSort;
    
        public bool CanGroup => collectionView.CanGroup;
    
        public ObservableCollection GroupDescriptions => collectionView.GroupDescriptions;
    
        public ReadOnlyObservableCollection Groups => collectionView.Groups;
    
        public bool IsEmpty => collectionView.IsEmpty;
    
        public object CurrentItem => collectionView.CurrentItem;
    
        public int CurrentPosition => collectionView.CurrentPosition;
    
        public bool IsCurrentAfterLast => collectionView.IsCurrentAfterLast;
    
        public bool IsCurrentBeforeFirst => collectionView.IsCurrentBeforeFirst;
    
        public NewItemPlaceholderPosition NewItemPlaceholderPosition
        {
            get => collectionView.NewItemPlaceholderPosition;
            set => collectionView.NewItemPlaceholderPosition = value;
        }
    
        public bool CanAddNew => collectionView.CanAddNew;
    
        public bool IsAddingNew => collectionView.IsAddingNew;
    
        public object CurrentAddItem => collectionView.CurrentAddItem;
    
        public bool CanRemove => collectionView.CanRemove;
    
        public bool CanCancelEdit => collectionView.CanCancelEdit;
    
        public bool IsEditingItem => collectionView.IsEditingItem;
    
        public object CurrentEditItem => collectionView.CurrentEditItem;
    
        #endregion
    
        #region Events
    
        public event NotifyCollectionChangedEventHandler CollectionChanged
        {
            add => ((ICollectionView) collectionView).CollectionChanged += value;
            remove => ((ICollectionView) collectionView).CollectionChanged -= value;
        }
    
        public event CurrentChangingEventHandler CurrentChanging
        {
            add => ((ICollectionView) collectionView).CurrentChanging += value;
            remove => ((ICollectionView) collectionView).CurrentChanging -= value;
        }
    
        public event EventHandler CurrentChanged
        {
            add => ((ICollectionView) collectionView).CurrentChanged += value;
            remove => ((ICollectionView) collectionView).CurrentChanged -= value;
        }
    
        #endregion
    
        #region Constructors
    
        public GenericCollectionView([NotNull] ListCollectionView collectionView)
        {
            this.collectionView = collectionView ?? throw new ArgumentNullException(nameof(collectionView));
        }
    
        #endregion
    
        #region Methods
    
        public IEnumerator GetEnumerator()
        {
            return (IEnumerator) ((ICollectionView) collectionView).GetEnumerator();
        }
    
        IEnumerator IEnumerable.GetEnumerator()
        {
            return ((ICollectionView) collectionView).GetEnumerator();
        }
    
        public bool Contains([NotNull] object item)
        {
            return collectionView.Contains(item);
        }
    
        public void Refresh()
        {
            collectionView.Refresh();
        }
    
        public IDisposable DeferRefresh()
        {
            return collectionView.DeferRefresh();
        }
    
        public bool MoveCurrentToFirst()
        {
            return collectionView.MoveCurrentToFirst();
        }
    
        public bool MoveCurrentToLast()
        {
            return collectionView.MoveCurrentToLast();
        }
    
        public bool MoveCurrentToNext()
        {
            return collectionView.MoveCurrentToNext();
        }
    
        public bool MoveCurrentToPrevious()
        {
            return collectionView.MoveCurrentToPrevious();
        }
    
        public bool MoveCurrentTo(object item)
        {
            return collectionView.MoveCurrentTo(item);
        }
    
        public bool MoveCurrentToPosition(int position)
        {
            return collectionView.MoveCurrentToPosition(position);
        }
    
        public object AddNew()
        {
            return collectionView.AddNew();
        }
    
        public void CommitNew()
        {
            collectionView.CommitNew();
        }
    
        public void CancelNew()
        {
            collectionView.CancelNew();
        }
    
        public void RemoveAt(int index)
        {
            collectionView.RemoveAt(index);
        }
    
        public void Remove([NotNull] object item)
        {
            collectionView.Remove(item);
        }
    
        public void EditItem(object item)
        {
            collectionView.EditItem(item);
        }
    
        public void CommitEdit()
        {
            collectionView.CommitEdit();
        }
    
        public void CancelEdit()
        {
            collectionView.CancelEdit();
        }
    
        #endregion
    }
    
    
    

    And finally the usage:

    ICollectionView PersonCollectionView { get; }
    

    In the constructor:

    var view = (ListCollectionView) CollectionViewSource.GetDefaultView(PersonCollection);
    PersonCollectionView = new GenericCollectionView(view);
    

    提交回复
    热议问题