当前位置: 首页 > 编程语言 > C#

ObservableCollectionElementTransformation-ProjectionWrapper分享

时间:2023-04-11 10:47:09 C#

ObservableCollectionElementTransformation/ProjectionWrapper在WPF中创建ViewModel时,有时需要转换ObservableCollection(源集合)中可用的数据来扩展/限制/投影原始元素(目标集合)包装元素集合,同时将数据和顺序转换为元素始终反映原始集合。就像Select扩展方法一样,只是它会不断更新,以便可以用于WPF绑定。如果在索引x处将元素添加到源,则在目标集合中的相同索引x处添加相同元素的包装器。如果索引y处的元素已从源集合中移除,则索引y处的元素也会从目标集合中移除。假设有一个ObservableCollection,但我需要绑定的是一个ReadOnlyObservableCollection(或等效的),其中ClassB->ClassA如下:(包装为INotifyPropertyChanged).PropertyChanged+=WrappedChanged;}publicClassAWrapped{get;私有集;}publicintSomeOtherProperty{get{returnSomeFunction(Wrapped);}WrappedChanged(objects,NotifyPropertyChangedArgsa){...}...}我可以写我自己的TemplatedTransformCollectionWrapper,我可以这样写:ObservableCollectionsource;TemplatedTransformCollectionWrappertheCollectionThatWillBeUsedInABinding=TemplatedTransformCollectionWrapper(source,classA=>newClassB(classA));TemplatedTransformCollectionWrapper理想地包装所有实现INotifyCollectionChanged的??集合,并正确处理所有可能的添加、删除原始包装的集合,以替换操作。正确编写一个TemplatedTransformCollectionWrapper并不是一件容易的事,这似乎是别人已经做过的事情,甚至可能是核心框架的一部分。但我找不到它。我在这里发布我的解决方法-这是一个自定义类。还是希望有更好的答案。以上就是C#学习教程:ObservableCollection元素转换/投影包装器分享的全部内容。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注——usingSystem;使用系统集合;使用系统。使用System.Collections.ObjectModel;使用System.Collections.Specialized;使用System.Linq;命名空间ViewLayer{publicclassTransformObservableCollection:INotifyCollectionChanged,IList,IReadOnlyList,IDisposable{publicTransformObservableCollection(ObservableCollectionmrappedCollection)wrappedCollection;m_TransformFunc=变换;((INotifyCollectionChanged)m_WrappedCollection).CollectionChanged+=TransformObservableCollection_CollectionChanged;m_TransformedCollection=newObservableCollection(m_WrappedCollection.Select(m_TransformFunc));)m_WrappedCollection).CollectionChanged-=TransformObservableCollection_CollectionChanged;m_WrappedCollection=null;}voidTransformObservableCollection_CollectionChanged(objectsender,NotifyCollectionChangedEventArgse){switch(e.Action){caseNotifyCollectionChangedAction.Add:if(e.NewItems==null||e.NewItems.Count!=1)中断;m_TransformedCollection.Insert(e.NewStartingIndex,m_TransformFunc((Source)e.NewItems[0]));返回;caseNotifyCollectionChangedAction.Move:if(e.NewItems==null||e.NewItems.Count!=1||e.OldItems==null||e.OldItems.Count!=1)中断;m_TransformedCollection.Move(e.OldStartingIndex,e.NewStartingIndex);返回;caseNotifyCollectionChangedAction.Remove:if(e.OldItems==null||e.OldItems.Count!=1)中断;m_TransformedCollection.RemoveAt(e.OldStartingIndex);返回;caseNotifyCollectionChangedAction.Replace:if(e.NewItems==null||e.NewItems.Count!=1||e.OldItems==null||e.OldItems.Count!=1||e.OldStartingIndex!=e.NewStartingIndex)中断;m_TransformedCollection[e.OldStartingIndex]=m_TransformFunc((Source)e.NewItems[0]);返回;}//这很可能是在Clear()上调用的,我们还没有优化其他情况m_TransformedCollection.Clear();foreach(m_WrappedCollection中的var项目)m_TransformedCollection.Add(m_TransformFunc(item));}#regionIList编辑不受支持的函数,因为此集合是只读的publicintAdd(objectvalue){thrownewInvalidOperationException();}publicvoidClear(){thrownewInvalidOperationException();}publicvoidInsert(intindex,objectvalue){thrownewInvalidOperationException();}publicvoidRemove(objectvalue){thrownewInvalidOperationException();}publicvoidRemoveAt(intindex){thrownewInvalidOperationException();}#endregionIList编辑不支持的函数,因为此集合是只读的#regionAccessorspublicTthis[intindex]{get{returnm_TransformedCollection[index];}}objectIList.this[intindex]{get{返回m_TransformedCollection[索引];}设置{抛出新的InvalidOperationException();}}publicboolContains(Tvalue){returnm_TransformedCollection.Contains(value);}boolIList.Contains(objectvalue){return((IList)m_TransformedCollection).Contains(value);}publicintIndexOf(Tvalue){returnm_TransformedCollection.IndexOf(value);}intIList.IndexOf(objectvalue){return((IList)m_TransformedCollection).IndexOf(value);}publicintCount{get{返回m_TransformedCollection.Count;}}publicIEnumeratorGetEnumerator(){返回m_TransformedCollection.GetEnumerator();}IEnumeratorIEnumerable.GetEnumerator(){返回((IEnumerable)m_TransformedCollection).GetEnumerator();}#endregionAccessorspublicboolIsFixedSize{get{returnfalse;}}publicboolIsReadOnly{get{returntrue;}}publicvoidCopyTo(Arrayarray,intindex){((IList)m_TransformedCollection).CopyTo(array,index);}publicvoidCopyTo(T[]array,intindex){m_TransformedCollection.CopyTo(数组,索引);}publicboolIsSynchronized{get{returnfalse;}}publicobjectSyncRoot{get{returnm_TransformedCollection;}}ObservableCollectionm_TransformedCollection;ObservableCollectionm_WrappedCollection;Funcm_TransformFunc;事件NotifyCollectionChangedEventHandlerINotifyCollectionChanged.CollectionChanged{添加{((INotifyCollectionChanged)m_TransformedCollection).CollectionChanged+=value;}删除{((INotifyCollectionChanged)m_TransformedCollection).CollectionChanged-=值;}}}}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。转载请注明出处:

最新推荐
猜你喜欢