你想要做什麼是深拷貝名單,所以你可以使用這個擴展:
/// <summary>
/// Reference Article http://www.codeproject.com/KB/tips/SerializedObjectCloner.aspx
///
/// Provides a method for performing a deep copy of an object.
/// Binary Serialization is used to perform the copy.
/// </summary>
public static class ObjectCopier
{
/// <summary>
/// Perform a deep Copy of the object.
/// </summary>
/// <typeparam name="T">The type of object being copied.</typeparam>
/// <param name="source">The object instance to copy.</param>
/// <returns>The copied object.</returns>
public static T Clone<T>(T source)
{
if (!typeof(T).IsSerializable)
{
throw new ArgumentException("The type must be serializable.", "source");
}
// Don't serialize a null object, simply return the default for that object
if (Object.ReferenceEquals(source, null))
{
return default(T);
}
IFormatter formatter = new BinaryFormatter();
Stream stream = new MemoryStream();
using (stream)
{
formatter.Serialize(stream, source);
stream.Seek(0, SeekOrigin.Begin);
return (T)formatter.Deserialize(stream);
}
}
}
現在,我可能會問你爲什麼這樣做,你爲什麼不通過名單另一個線程和多數民衆贊成在所有(鑑於你修改它回來)。如果它是一個UI控件(可能是我看到的),那麼可以使用背景ItemsSource(來自其他線程),然後使用Dispatcher在UI上將其用作源。如果這就是你想讓我知道提供更多細節。
只是不。只在線程中創建數據。填充它,比如說一個List <>。只在UI線程上填充ListView。 BackgroundWorker使其變得簡單。 – 2012-04-22 17:39:23
是的,@Hans說了什麼。必須使用適當的數據源。 – IAbstract 2012-04-22 17:54:32
這就是我所做的。我只想複製整個ListView,因爲結構已經存在。我剛剛創建了一個具有相同列的新結構,然後將其逐行地複製到實際的結構中。 – CJxD 2012-05-02 07:53:03