Convert List to ObservableCollection in WP7

后端 未结 10 930
不知归路
不知归路 2020-12-25 10:10

I don\'t know if it\'s just too late or what, but I don\'t see how to do this...

What I\'m expecting to do, and what the object browser says is there, is this:

10条回答
  •  心在旅途
    2020-12-25 10:41

    You'll have to write your own extension method to do this:

        public static class CollectionEx
        {
          /// 
          /// Copies the contents of an IEnumerable list to an ObservableCollection
          /// 
          /// The type of objects in the source list
          /// The source list to be converted
          /// An ObservableCollection containing the objects from the source list
          public static ObservableCollection ToObservableCollection( this IEnumerable enumerableList )
          {
            if( enumerableList != null ) {
              // Create an emtpy observable collection object
              var observableCollection = new ObservableCollection();
    
              // Loop through all the records and add to observable collection object
              foreach( var item in enumerableList ) {
                observableCollection.Add( item );
              }
    
              // Return the populated observable collection
              return observableCollection;
            }
            return null;
          }
        }
    

提交回复
热议问题