gpt4 book ai didi

c# - ObservableCollection 逐元素变换/投影包装器

转载 作者:可可西里 更新时间:2023-11-01 07:58:37 25 4
gpt4 key购买 nike

在 WPF 中创建 ViewModel 时,有时需要转换 ObservableCollection 中可用的数据(源集合)转换为扩展/限制/转换原始元素(目标集合)的包装元素集合,而元素的数量和顺序始终反射(reflect)原始集合。

就像Select扩展方法,但它会不断更新,因此可用于 WPF 绑定(bind)。

如果一个元素被添加到索引 x 处的源中,则相同元素的 Wrapper 被添加到目标集合中的相同索引 x 处。如果索引 y 的元素在源集合中被删除,则索引 y 的元素在目标集合中被删除。

假设有一个 ObservableCollection<ClassA> ,但我需要绑定(bind)的是 ReadOnlyObservableCollection<ClassB> (或等效的),其中 ClassB -> ClassA如下:

class ClassB : INotifyPropertyChanged, IDisposable
{
public ClassB(ClassA a)
{
Wrapped = a;
(Wrapped as INotifyPropertyChanged).PropertyChanged+=WrappedChanged;
}
public ClassA Wrapped { get; private set; }
public int SomeOtherProperty { get { return SomeFunction(Wrapped); }
WrappedChanged(object s, NotifyPropertyChangedArgs a) { ... }
...
}

我可以自己写TemplatedTransformCollectionWrapper ,我可以在哪里写这个:

ObservableCollection<ClassA> source;
TemplatedTransformCollectionWrapper theCollectionThatWillBeUsedInABinding
= TemplatedTransformCollectionWrapper(source, classA => new ClassB(classA));

TemplatedTransformCollectionWrapper 理想地包装所有实现 INotifyCollectionChanged 的集合并正确处理所有可能的添加、删除、替换原始、包装、集合的操作。

TemplatedTransformCollectionWrapper 并不简单正确,这似乎是其他人已经做过的事情,甚至可能是核心框架的一部分。但是我找不到它。

最佳答案

我将在此处发布我的解决方法 - 这是一个自定义类。仍然希望得到更好的答案。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;

namespace ViewLayer
{
public class TransformObservableCollection<T,Source> : INotifyCollectionChanged, IList, IReadOnlyList<T>, IDisposable
{
public TransformObservableCollection(ObservableCollection<Source> wrappedCollection, Func<Source,T> transform)
{
m_WrappedCollection = wrappedCollection;
m_TransformFunc = transform;
((INotifyCollectionChanged)m_WrappedCollection).CollectionChanged += TransformObservableCollection_CollectionChanged;
m_TransformedCollection = new ObservableCollection<T>(m_WrappedCollection.Select(m_TransformFunc));
}
public void Dispose()
{
if (m_WrappedCollection == null) return;
((INotifyCollectionChanged)m_WrappedCollection).CollectionChanged -= TransformObservableCollection_CollectionChanged;
m_WrappedCollection = null;
}
void TransformObservableCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
if (e.NewItems == null || e.NewItems.Count != 1)
break;
m_TransformedCollection.Insert(e.NewStartingIndex,m_TransformFunc((Source)e.NewItems[0]));
return;
case NotifyCollectionChangedAction.Move:
if (e.NewItems == null || e.NewItems.Count != 1 || e.OldItems == null || e.OldItems.Count != 1)
break;
m_TransformedCollection.Move(e.OldStartingIndex, e.NewStartingIndex);
return;
case NotifyCollectionChangedAction.Remove:
if (e.OldItems == null || e.OldItems.Count != 1)
break;
m_TransformedCollection.RemoveAt(e.OldStartingIndex);
return;
case NotifyCollectionChangedAction.Replace:
if (e.NewItems == null || e.NewItems.Count != 1 || e.OldItems == null || e.OldItems.Count != 1 || e.OldStartingIndex != e.NewStartingIndex)
break;
m_TransformedCollection[e.OldStartingIndex] = m_TransformFunc((Source)e.NewItems[0]);
return;
} // This is most likely called on a Clear(), we don't optimize the other cases (yet)
m_TransformedCollection.Clear();
foreach (var item in m_WrappedCollection)
m_TransformedCollection.Add(m_TransformFunc(item));
}

#region IList Edit functions that are unsupported because this collection is read only
public int Add(object value) { throw new InvalidOperationException(); }
public void Clear() { throw new InvalidOperationException(); }
public void Insert(int index, object value) { throw new InvalidOperationException(); }
public void Remove(object value) { throw new InvalidOperationException(); }
public void RemoveAt(int index) { throw new InvalidOperationException(); }
#endregion IList Edit functions that are unsupported because this collection is read only

#region Accessors
public T this[int index] { get { return m_TransformedCollection[index]; } }
object IList.this[int index] { get { return m_TransformedCollection[index]; } set { throw new InvalidOperationException(); } }
public bool Contains(T value) { return m_TransformedCollection.Contains(value); }
bool IList.Contains(object value) { return ((IList)m_TransformedCollection).Contains(value); }
public int IndexOf(T value) { return m_TransformedCollection.IndexOf(value); }
int IList.IndexOf(object value) { return ((IList)m_TransformedCollection).IndexOf(value); }
public int Count { get { return m_TransformedCollection.Count; } }
public IEnumerator<T> GetEnumerator() { return m_TransformedCollection.GetEnumerator(); }
IEnumerator IEnumerable.GetEnumerator() { return ((IEnumerable)m_TransformedCollection).GetEnumerator(); }
#endregion Accessors

public bool IsFixedSize { get { return false; } }
public bool IsReadOnly { get { return true; } }
public void CopyTo(Array array, int index) { ((IList)m_TransformedCollection).CopyTo(array, index); }
public void CopyTo(T[] array, int index) { m_TransformedCollection.CopyTo(array, index); }
public bool IsSynchronized { get { return false; } }
public object SyncRoot { get { return m_TransformedCollection; } }

ObservableCollection<T> m_TransformedCollection;
ObservableCollection<Source> m_WrappedCollection;
Func<Source, T> m_TransformFunc;

event NotifyCollectionChangedEventHandler INotifyCollectionChanged.CollectionChanged
{
add { ((INotifyCollectionChanged)m_TransformedCollection).CollectionChanged += value; }
remove { ((INotifyCollectionChanged)m_TransformedCollection).CollectionChanged -= value; }
}
}
}

关于c# - ObservableCollection 逐元素变换/投影包装器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13920703/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com