tags:

views:

92

answers:

2

I have an ObservableCollection assigned to People and have a PersonViewModel and a PeopleViewModel

_people= GetAll().ToList();
List<Person> allPeople = (from person in _people 
                          select new PersonViewModel(person)).ToList();
AllPeople = new ObservableCollection<WorkOrderListItemViewModel>(allOrders);
AllPeopleCollection.Source = AllPeople;

where
AllPeopleCollection is a Public Property of type CollectionViewSource and
AllPeople is a Public Property of type ObservableCollection

i need to change the icon i use for a row in a listview on click of that item. But to Update the View, I need to read the whole List again. As my List Has 100+ records, it's taking a long time to refresh the list.

Is there a way I can only Refresh a Particular Item in the List and Refresh it on the UI.

+2  A: 

Hi,

yes, your PersonViewModel should implement INotifyPropertyChanged and raise the PropertyChanged event in the setter of the icon property (so it's raised automatically after every update). This will trigger a refresh of the GUI if your bindings are correct.

Code:

public class PersonViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private YourIconType _Icon;
    public YourIconType Icon
    {
        get { return _Icon; }
        set
        {
            _Icon = value;
            if (PropertyChanged != null)
                PropertyChanged.Invoke(this, 
                    new PropertyChangedEventArgs("Icon"));
        }
    }
}
andyp
@andyp I am already Implementing that in my base class.. and triggering the Onproperty Changed for the Collection
crazy9
yes, but that just updates if the collection itself changes (if an item is added or removed) and not single items if the property of an item changes.
andyp
A: 

First of all, your record count is really nothing in front of wpf's ListView as it makes use of the VirtualizingStackPanel.

Apart from the exact solution of PropertyChange Notification, you should also consider having a look at the concept of deferred execution. You seem to convert everything into a list which would lead to force enumeration of the result set at once.

Let us consider your code:

_people= GetAll().ToList();  
//The result of GetAll is enumerated and a solid list is created

List<Person> allPeople = (from person in _people 
                          select new PersonViewModel(person)).ToList(); 
// another list created

AllPeople = new ObservableCollection<PersonViewModel>(allPeople); 
// ObservableCollection created out of list

Let us tweak it a bit:

_people= GetAll(); // The result is just referred by _people

IEnumerable<Person> allPeople = (from person in _people 
                                 select new PersonViewModel(person)); 
// IEnumerable is just provided with a query. No other operation is done

AllPeople = new ObservableCollection<PersonViewModel>(allPeople); 
// The ObservableCollection requests element from allPeople 
// which in turn requests from the query 
// which in turn requests from _people 
// which enumerates the result of GetAll() and yields the result. 

Hence you can avoid the creation of temporary lists.

Further, even the GetAll() method could be made to return an IEnumerable if it doesn't.

You could take a look at
IEnumerable
IQueryable
yield

Veer
@Veer Thanks for the suggestion to make the List into IEnumerable Collection.My Main concern comes HereIEnumerable<Person> allPeople = (from person in _people select new PersonViewModel(person)); as I have lot of methods calling to set Properties on the PersonViewModel.It use to take a lot of time for each record to go through all methods.But now after Putting it to IEnumerable it doesnt take much timebut when the debug point goes to AllPeople = new ObservableCollection<PersonViewModel>(allPeople); line it takes some noticible time to load
crazy9
@user311945: At one point anyways the solid list has to be created and hence the time. But it should not take much time as you specify. Should be less than a second for thousands of items. Can you post the code for PersonViewModel constructor and other things that you feel consume time. Use the Stopwatch class.
Veer