ListView update after adding an item

I am new to Windows development and, of course, new to Metro style app development. I'm not sure I understand how data binding works.

I have a list of items.

private List<Expense> _expenses = new List<Expense>();
public List<Expense> Items
{
    get
    {
        return this._expenses;
    }
}

What I associate with XAML. (I am using a split page template)

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        this.DefaultViewModel["Items"] = _data.Items;
    }

Then i show it

<UserControl.Resources>
    <CollectionViewSource
        x:Name="itemsViewSource"
        Source="{Binding Items, Mode=TwoWay}"/>
</UserControl.Resources>

<ListView
    x:Name="itemListView"
    AutomationProperties.AutomationId="ItemsListView"
    AutomationProperties.Name="Items"
    Margin="120,0,0,60"
    ItemsSource="{Binding Source={StaticResource itemsViewSource}}"
    SelectionChanged="ItemListView_SelectionChanged"
    ItemTemplate="{StaticResource DefaultListItemTemplate}"/>

Which works great. Then, when the user clicks the “I” button, I add a new item to my list

_data.Items.Add(new Expense
{
    Total = 100,
    When = new DateTime(2013, 6, 6),
    For = "Myself"
});

I expected it to ListViewbe updated automatically since I install Mode=TwoWay, but it is not. Did I not understand the concept, and the list cannot be updated? Otherwise, what could I do wrong?

+3
source share
1 answer

, INotifyCollectionChanged. , , , .

. ObservableCollection<T> List<T>,

private ObservableCollection<Expense> _expenses = new ObservableCollection<Expense>();
public ObservableCollection<Expense> Items
{
    get
    {
        return this._expenses;
    }
}
+7

All Articles