这是将List <t>从模型转换为视图中的ObservableCollection <t>的最佳方法吗?</t> </t>

时间:2010-03-05 15:47:46

标签: c# mvvm generics observablecollection

MVVM 开发中,我不断将 List<T> 模型转换为 ObservableCollection<T> 用于观看次数

在.NET中查看,以便简明扼要地执行此操作,例如:例如.ToList<>.ToArray<>.ToDictionary<>,但找不到与 ObservableCollection 相似的内容。

因此我制作了以下扩展方法 ConvertToObservableCollection<T>()

有没有更好的方法将List<T>转换为ObservableCollection<T>,或者每个MVVM开发人员最终都会在某个时候编写此扩展方法?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Documents;
using System.Collections.ObjectModel;

namespace TestObser228342
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            List<string> names = new List<string> { "one", "two", "three" };
            ObservableCollection<string> ocNames = 
                names.ConvertToObservableCollection<string>();
            ocNames.ToList().ForEach(n => Console.WriteLine(n));

            List<Customer> customers = new List<Customer>
            {
                new Customer { FirstName = "Jim", LastName = "Smith" },
                new Customer { FirstName = "Jack", LastName = "Adams" },
                new Customer { FirstName = "Collin", LastName = "Rollins" }
            };
            ObservableCollection<Customer> ocCustomers = 
                customers.ConvertToObservableCollection<Customer>();
            ocCustomers.ToList().ForEach(c => Console.WriteLine(c));
        }
    }

    public static class StringHelpers
    {
        public static ObservableCollection<T> ConvertToObservableCollection<T>
            (this List<T> items)
        {
            ObservableCollection<T> oc = new ObservableCollection<T>();
            foreach (var item in items)
            {
                oc.Add(item);
            }
            return oc;
        }
    }

    public class Customer
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public override string ToString()
        {
            return FirstName + " " + LastName;
        }
    }

}

4 个答案:

答案 0 :(得分:12)

为什么不使用ObservableCollection的适当构造函数?

ObservableCollection<Customer> ocCustomers = 
         new ObservableCollection<Customer>(customers);

答案 1 :(得分:1)

使用ObservableCollection构造函数声明变量也适用于Silverlight 4。

答案 2 :(得分:0)

答案 3 :(得分:0)

你的解决方案似乎很复杂......也许我的太简单了......这是我写的一个扩展方法,用于将我的nettiers集合转换为可观察的集合......它在vb.net中...但你会得到要点...

<System.Runtime.CompilerServices.Extension()> _
    Public Function [ToObservableCollection](Of T)(ByVal list As IEnumerable(Of T)) As ObservableCollection(Of T)
        Dim collection As New ObservableCollection(Of T)

        For Each l As T In list
            collection.Add(l)
        Next

        Return collection
    End Function

这里是转换后的C#

[System.Runtime.CompilerServices.Extension()]
public ObservableCollection<T> ToObservableCollection<T>(IEnumerable<T> list)
{
    ObservableCollection<T> collection = new ObservableCollection<T>();
    
    foreach (T l in list) {
        collection.Add(l);
    }
    
    return collection;
}

我想你可以使用lambda,但我不理解它们,所以我避免使用它们。