Mcloving
Mcloving

Reputation: 1410

ListView Not updating if a delete an item

I have a listView which is set to Mode=TwoWay, which i thought was suppose to refresh the views, if the underlying data changed.

However, while the item is deleted correctly, the item still remains on the list, until I exit and return to the page.

Xaml:

<ListView ItemsSource="{Binding Path=items, Mode=TwoWay}" >
    <DataTemplate>
       ...

        <Button   x:Name="btn_delete_item" Click="btn_delete_item_Click" >

        </Button>

Behind code:

 private void btn_delete_item_Click(object sender, RoutedEventArgs e)
    {
        Button button = sender as Button;
        itemType item = button.DataContext as itemType;

        items.Remove(item);
    }

Upvotes: 2

Views: 2658

Answers (1)

Clemens
Clemens

Reputation: 128061

In order to fully support data binding, your Items collection must notify about changes, i.e. whether items are added, removed, replaced or moved. This notification is done by implementing the INotifyCollectionChanged interface. The framework's List<T> type does not implement this interface, but ObservableCollection<T> does.

So you could simply change the type of your Items property:

public ObservableCollection<ItemType> Items { get; set; }

Upvotes: 4

Related Questions