0

我在我的 MainPage 中使用了一个 Expander View,它绑定到“帐户类别”的集合(此集合中的每个项目都有一个帐户集合)

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

绑定在主页的 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>

有什么想法吗?提前感谢您的帮助..

4

1 回答 1

0

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

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

于 2012-01-31T14:55:01.793 回答