Collection Bind to expanded View won't update when new Items are added

348 views Asked by At

I am using a Expander View in my MainPage that is Bind to a collection of "Account Categories" (each item in this collection has further a collection of Accounts)

The Bindings are all working fine, with a small glitch though. There is another page Where a user can Add new Accounts (Thus changing Accounts & Account Categories) now when I navigate back to the Main Page the Expander Control does not show updated values?

The binding is done on OnNavigatedTo event of the Main Page The DataBase context file is generated by Sql Metal tool (More on this here Using SQl Metal to generate DB files for wp7)

That means all my classes implement INotifyChanging & INotifyChangedEvents

Here is the XAML & C# code

private WalletDataContext context;

    private ObservableCollection<AccountCategory> _accountCategories;
    public ObservableCollection<AccountCategory> AccountCategories
    {
        get { return _accountCategories; }
        set
        {
            if (_accountCategories != value)
            {
                _accountCategories = value;
                NotifyPropertyChanged("AccountCategories");
            }
        }
    }

public MainPage()
    {
        InitializeComponent();

        //Initialize Data context
        context = new WalletDataContext(WalletDataContext.DBConnectionString);

        //Set page data context
        this.DataContext = this;
    }

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        //fetch all existing Account Categories
        var accountCategoriesInDB = (from AccountCategory acctCat in context.AccountCategory
                                     select acctCat);

        //Update Page Collection
        AccountCategories = new ObservableCollection<AccountCategory>(accountCategoriesInDB);

        this.listBox.ItemsSource = AccountCategories;

        base.OnNavigatedTo(e);

    }

Here are the XAML bindings

<ListBox Grid.Row="0" x:Name="listBox">
                    <ListBox.ItemContainerStyle>
                        <Style TargetType="ListBoxItem">
                            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                        </Style>
                    </ListBox.ItemContainerStyle>

                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel/>
                        </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <toolkit:ExpanderView Header="{Binding}" Expander="{Binding}"
                            ItemsSource="{Binding Accounts}"
                            HeaderTemplate="{StaticResource CustomHeaderTemplate}" ExpanderTemplate="{StaticResource CustomExpanderTemplate}">
                                <toolkit:ExpanderView.ItemTemplate>
                                    <DataTemplate>
                                        <Grid VerticalAlignment="Center" Height="Auto">
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="0.105*"/>
                                                <RowDefinition Height="0.105*"/>
                                                <RowDefinition Height="0.789*"/>
                                            </Grid.RowDefinitions>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="0.366*"/>
                                                <ColumnDefinition Width="0.634*"/>
                                            </Grid.ColumnDefinitions>

                                            <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding AccountNumber}" Style="{StaticResource PhoneTextNormalStyle}" VerticalAlignment="Center" HorizontalAlignment="Left"></TextBlock>
                                            <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Balance}" Style="{StaticResource PhoneTextSubtleStyle}" VerticalAlignment="Center" HorizontalAlignment="Left"></TextBlock>
                                        </Grid>
                                    </DataTemplate>
                                </toolkit:ExpanderView.ItemTemplate>
                            </toolkit:ExpanderView>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

Any ideas on whats wrong? Thanks for your help in advance..

1

There are 1 answers

3
Emond On

The MainPage reloads the data from the context in the OnNavigatedTo method.

Make sure you save/mark the updated data to the context in the Add New Account page.