Convert List to ObservableCollection in WP7

后端 未结 10 851
不知归路
不知归路 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:34

    ObservableCollection has several constructors which have input parameter of List<T> or IEnumerable<T>:
    List<T> list = new List<T>();
    ObservableCollection<T> collection = new ObservableCollection<T>(list);

    0 讨论(0)
  • 2020-12-25 10:35

    If you are going to be adding lots of items, consider deriving your own class from ObservableCollection and adding items to the protected Items member - this won't raise events in observers. When you are done you can raise the appropriate events:

    public class BulkUpdateObservableCollection<T> : ObservableCollection<T>
    {
        public void AddRange(IEnumerable<T> collection)
        {
            foreach (var i in collection) Items.Add(i);
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
            OnPropertyChanged(new PropertyChangedEventArgs("Count"));
        }
     }
    

    When adding many items to an ObservableCollection that is already bound to a UI element (such as LongListSelector) this can make a massive performance difference.

    Prior to adding the items, you could also ensure you have enough space, so that the list isn't continually being expanded by implementing this method in the BulkObservableCollection class and calling it prior to calling AddRange:

        public void IncreaseCapacity(int increment)
        {
            var itemsList = (List<T>)Items;
            var total = itemsList.Count + increment;
            if (itemsList.Capacity < total)
            {
                itemsList.Capacity = total;
            }
        }
    
    0 讨论(0)
  • 2020-12-25 10:37

    The answer provided by Zin Min solved my problem with a single line of code. Excellent!

    I was having the same issue of converting a generic List to a generic ObservableCollection to use the values from my List to populate a ComboBox that is participating in binding via a factory class for a WPF Window.

    _expediteStatuses = new ObservableCollection<ExpediteStatus>(_db.getExpediteStatuses());

    Here is the signature for the getExpediteStatuses method:

    public List<ExpediteStatus> getExpediteStatuses()

    0 讨论(0)
  • 2020-12-25 10:41

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

        public static class CollectionEx
        {
          /// <summary>
          /// Copies the contents of an IEnumerable list to an ObservableCollection
          /// </summary>
          /// <typeparam name="T">The type of objects in the source list</typeparam>
          /// <param name="enumerableList">The source list to be converted</param>
          /// <returns>An ObservableCollection containing the objects from the source list</returns>
          public static ObservableCollection<T> ToObservableCollection<T>( this IEnumerable<T> enumerableList )
          {
            if( enumerableList != null ) {
              // Create an emtpy observable collection object
              var observableCollection = new ObservableCollection<T>();
    
              // 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;
          }
        }
    
    0 讨论(0)
提交回复
热议问题