2012-01-31 83 views
0

我在我的MainPage中使用了一个展开器视图绑定到“帐户类别”的集合(此集合中的每个项目都有一个帐户集合)集合绑定到展开视图不会更新添加新项目时

绑定工作正常,虽然有一个小毛刺。还有另一个页面,当我回到主页面时,现在当用户可以添加新帐户(因此更改帐户类别&帐户类别)扩展器控件不显示更新值?

在主画面 由SQL金属工具 (关于这一点在这里Using SQl Metal to generate DB files for wp7)生成的数据库环境文件的OnNavigatedTo事件进行绑定

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

有什么想法是什么错? 感谢您的帮助。

回答

0

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

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

+0

“请确保将更新的数据保存/标记到添加新帐户页面中的上下文”我将数据保存在添加新帐户页面上。但是,当我回到主页面时,新输入的记录只是不可见。 – Supreet 2012-02-01 13:45:41

+0

您能否显示保存数据的代码? – 2012-02-01 13:57:30

+0

男孩有人请帮助这里..它的类型现在越过我的头.. :(感谢 – Supreet 2012-02-05 10:18:51

相关问题