添加新项目时,收集绑定到展开的视图不会更新

时间:2012-01-31 14:03:29

标签: windows-phone-7 binding sqlmetal

我在我的主页中使用扩展器视图绑定到“帐户类别”的集合(此集合中的每个项目还有一个帐户集合)

绑定工作正常,但有一个小故障。还有另一个页面当用户可以添加新帐户(因此更改帐户和帐户类别)时,当我导航回主页面时,扩展器控件未显示更新的值?

绑定是在主页面的OnNavigatedTo事件上完成的 DataBase上下文文件由Sql Metal工具生成 (更多相关信息Using SQl Metal to generate DB files for wp7

这意味着我的所有课程都实现了INotifyChanging& INotifyChangedEvents

这是XAML& C#代码

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);

    }

以下是XAML绑定

<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>

关于什么错误的想法? 感谢您的帮助..

1 个答案:

答案 0 :(得分:0)

MainPage从OnNavigatedTo方法的上下文重新加载数据。

确保将更新的数据保存/标记到“添加新帐户”页面中的上下文。