Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My CollectionViewSource is not picking up changes

I have a ListView which I'm binding to a CollectionViewSource in code behind with:

collectionView = CollectionViewSource.GetDefaultView(TableView.ItemsSource);
collectionView.SortDescriptions.Clear();
collectionView.SortDescriptions.Add(new SortDescription(propertyName, direction));

The TableView is the ListView, the propertyName is the name of the column I want sorted, and direction is either ascending or descending.

The XAML has the following for ItemSource:

ItemsSource="{Binding Rows}"

The code behind has the following for the Rows:

List<TableRow> rows;

public List<TableRow> Rows
{
    get { return rows; }
    set 
    {
        rows = value;
        UpdateProperty("Rows");
    }
}

the update is as follows:

public void Update()
{
     ...generate a list of rows...

     Rows = ...rows...
}

The problem occurs when the Update is called, the list view does update, but loses the sorting set previously on the CollectionViewSource.

like image 593
imekon Avatar asked Nov 19 '25 03:11

imekon


2 Answers

If you are "newing" rows then any setting on the prior rows is gone. If you clear (not new) the rows then I think they will hold the setting.

And you don't even want Rows = rows in update. After assign rows then.

NotifyPropertyChange("Rows"); 

So the UI know to update

If you are going to new then reassign

collectionView = CollectionViewSource.GetDefaultView(TableView.ItemsSource);
collectionView.SortDescriptions.Clear();
collectionView.SortDescriptions.Add(new SortDescription(propertyName, direction));

Maybe

private List<TableRow> rows = new List<TableRow>();  

and have that the only place you new it

like image 131
paparazzo Avatar answered Nov 21 '25 17:11

paparazzo


The answer is to reapply the sort descriptions after the update, as in:

collectionView = CollectionViewSource.GetDefaultView(TableView.ItemsSource);  
collectionView.SortDescriptions.Clear();
collectionView.SortDescriptions.Add(new SortDescription(propertyName, direction));

Then the sorting isn't lost. Refresh on the collection view doesn't help.

like image 20
imekon Avatar answered Nov 21 '25 18:11

imekon