Quantcast
Channel: Active questions tagged listview - Stack Overflow
Viewing all articles
Browse latest Browse all 611

Implementing a ListView with a CollectionViewSource - Not Refreshing?

$
0
0

I'm working on setting up a ListView whose Source property is set to an ivar of a class of mine, called Cat.

Each Cat has an ObservableCollection of Trait objects:

private ObservableCollection<Trait> _traits = new ObservableCollection<Trait>();public ObservableCollection<Trait> Traits{get    {        return _traits;    }}public void AddTrait(Trait t){    _traits.Add(t);    // Is this redundant? Is one better than the other?    this.OnPropertyChanged("_traits");    this.OnPropertyChanged("Traits");}public IEnumerator<Object> GetEnumerator(){    return _traits.GetEnumerator();}

And then I'm assigning the Source property to this Traits collection:

this.CollectionViewSource.Source = CurrentCat.Traits;

This works properly, and the Trait objects are properly displayed in my ListView.

The issue is that changes to this underlying _traits collection do not cause the UI to update properly. For example, this:

void AddTraitButton_Click(object sender, RoutedEventArgs e){    if (this.CurrentCat != null)    {        this.CurrentCat.AddTrait(new Trait());    }}

Doesn't seem to have any effect immediately in the UI, but if I reset the Source property like so:

var oldSource = this.CollectionViewSource.Source;this.CollectionViewSource.Source = null;this.CollectionViewSource.Source = oldSource;

Then the ListView updates properly. But, I'm sure there must be something that I'm missing, as I'd like for the UI to update upon the addition/removal of an item.

Edit: The CollectionViewSource is being applied to the ListView in my XAML file:

<CollectionViewSource x:Name="CollectionViewSource" x:Key="CollectionViewSource" />...<ListView x:Name="ItemListView" ItemsSource="{Binding Source={StaticResource CollectionViewSource}}" ...

Viewing all articles
Browse latest Browse all 611

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>