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



我在主页中使用的是绑定到"帐户类别"集合的扩展器视图(该集合中的每个项目都有一个帐户集合)

绑定都运行良好,不过有一个小故障。还有另一个页面,用户现在可以在其中添加新帐户(从而更改帐户和帐户类别)。当我导航回主页时,扩展器控件不显示更新的值?

绑定在主页的OnNavigatedTo事件上完成数据库上下文文件由Sql Metal工具生成(更多关于这一点,请点击这里使用SQl Metal为wp7生成DB文件)

这意味着我的所有类都实现了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>

有什么问题吗?谢谢你事先的帮助。。

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

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

最新更新