2012-01-31 41 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

相關問題