2012-09-13 86 views
1

我正在嘗試將對象添加到ObservableCollection。正如在這個網站的幾個問題中提到的,我甚至在添加項目之前嘗試實例化集合。但是,我仍然收到錯誤。這裏是我的觀察集合:將項目添加到ObservableCollection時出現NullReferenceException

//Datacontext for local database 
private WordDataContext wordsDB; 

//Observable collection for binding 
private ObservableCollection<WordItem> _wordItems = new ObservableCollection<WordItem>(); 
public ObservableCollection<WordItem> WordItems 
{ 
    get 
    { 
     return _wordItems; 
    } 
    set 
    { 
     if (_wordItems != value) 
     { 
      _wordItems = value; 
      NotifyPropertyChanged("WordItems"); 
     } 
    } 
} 

我已重寫的OnNavigatedTo

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) 
    { 
     // Define the query to gather all of the idea items. 
     var wordItemsInDB = from WordItem word in wordsDB.WordItems 
          select word; 

     // Execute the query and place the results into a collection. 
     WordItems = new ObservableCollection<WordItem>(wordItemsInDB); 

     // Call the base method. 
     base.OnNavigatedTo(e); 
    } 

這裏是按鈕添加新項

private void newIdeaAddButton_Click(object sender, RoutedEventArgs e) 
    { 
     //// Create a new idea item based on the text box. 
     //WordItem newIdea = new WordItem { WordName = "TestTest" }; 
     //Debug.WriteLine("I'm here!"); 
     //// Add a idea item to the observable collection. 
     //WordItems.Add(newIdea); 

     //// Add a idea item to the local database. 
     //wordsDB.WordItems.InsertOnSubmit(newIdea); 
     WordItem newword = new WordItem { WordName = "Bingo" }; 
     if (WordItems == null) 
     { 
      Debug.WriteLine("I'm null!"); 
      WordItems = new ObservableCollection<WordItem>(); 
     } 

     WordItems.Add(newword); 
     wordsDB.WordItems.InsertOnSubmit(newword); 
     Debug.WriteLine("Did something!"); 
    } 

而這裏的XAML

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
     <!--<ListBox Margin="14,0,-12,0" FontSize="{StaticResource PhoneFontSizeExtraLarge}" FontFamily="{StaticResource PhoneFontFamilySemiLight}"> 
      <ListBoxItem Content="About" Tap="GoToAbout"/> 
     </ListBox>--> 
     <telerikData:RadJumpList x:Name="TestList" IsStickyHeaderEnabled="True" Margin="14,0,-12,0" ItemsSource="{Binding WordItems}"> 
      <telerikData:RadJumpList.ItemTemplate> 
       <DataTemplate>              
        <StackPanel Orientation="Horizontal" Height="74">          
         <Rectangle x:Name="Bully" Width="20" Fill="Gray" Height="62" VerticalAlignment="Top" HorizontalAlignment="Left" /> 
         <TextBlock Style="{StaticResource PhoneTextExtraLargeStyle}" Text="{Binding WordItem}" VerticalAlignment="Top"/> 
        </StackPanel>       
       </DataTemplate> 
      </telerikData:RadJumpList.ItemTemplate> 

      <telerikData:RadJumpList.StickyHeaderTemplate>      
       <DataTemplate> 
        <Border HorizontalAlignment="Stretch" Background="{StaticResource PhoneBackgroundBrush}" Height="74"> 
         <Border Background="{StaticResource PhoneAccentBrush}" VerticalAlignment="Top" HorizontalAlignment="Left" Width="62" Height="62"> 
          <TextBlock Text="{Binding}" FontFamily="{StaticResource PhoneFontFamilySemiLight}" FontSize="{StaticResource PhoneFontSizeExtraLarge}" Padding="7,0,0,0" VerticalAlignment="Bottom" Foreground="White" /> 
         </Border> 
        </Border> 
       </DataTemplate> 
      </telerikData:RadJumpList.StickyHeaderTemplate> 

      <telerikData:RadJumpList.GroupHeaderTemplate> 
       <DataTemplate> 
        <Border HorizontalAlignment="Stretch" Background="{StaticResource PhoneBackgroundBrush}" Height="74"> 
         <Border Background="{StaticResource PhoneAccentBrush}" VerticalAlignment="Top" HorizontalAlignment="Left" Width="62" Height="62"> 
          <TextBlock Text="{Binding}" FontFamily="{StaticResource PhoneFontFamilySemiLight}" FontSize="{StaticResource PhoneFontSizeExtraLarge}" Padding="7,0,0,0" VerticalAlignment="Bottom" Foreground="White" /> 
         </Border> 
        </Border> 
       </DataTemplate> 
      </telerikData:RadJumpList.GroupHeaderTemplate> 

      <telerikData:RadJumpList.GroupPickerItemsPanel> 
       <ItemsPanelTemplate> 
        <toolkit:WrapPanel HorizontalAlignment="Center" ItemHeight="111" ItemWidth="111"/> 
       </ItemsPanelTemplate> 
      </telerikData:RadJumpList.GroupPickerItemsPanel> 

      <telerikData:RadJumpList.GroupPickerItemTemplate> 
       <DataTemplate> 
        <Border Background="{StaticResource PhoneAccentBrush}" Width="99" Height="99" VerticalAlignment="Top" HorizontalAlignment="Left"> 
         <TextBlock Text="{Binding}" Style="{StaticResource PhoneTextExtraLargeStyle}" VerticalAlignment="Bottom" Foreground="White" /> 
        </Border> 
       </DataTemplate> 
      </telerikData:RadJumpList.GroupPickerItemTemplate> 
     </telerikData:RadJumpList> 
     <Button x:Name="newIdeaAddButton" Click="newIdeaAddButton_Click" Content="Button" Height="72" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="160" /> 
    </Grid> 
+4

你究竟在哪一行得到'NullReferenceException'? – Spontifixus

+0

這與ObservableCollection無關。最有可能wordsDB爲空 –

+0

我同意 - wordsDB可能爲空。 –

回答

1

好吧,我終於得到了e解決方案!問題本身有點模糊。問題是,早些時候,我曾RadJumplist綁定到一個List<strings>和它GroupDescriptor相應地定義

GenericGroupDescriptor<string, string> testgroup = new GenericGroupDescriptor<string, string>(listitem => listitem.Substring(0, 1).ToLower()); 

然而,有問題的情況下約爲ObservableCollection<WordItem>。只要一個項目被添加到集合中,就會通知RadJumpList有關這些更改,並且GroupDescriptor在該上下文中被證明是無效的。這不知何故引發了NullReferenceException。將錯誤與原因聯繫起來有點不直觀。

所以,簡單的解決辦法是改變描述如下

GenericGroupDescriptor<WordItem, string> gengd = new GenericGroupDescriptor<WordItem, string>(); 
     gengd.KeySelector = (WordItem item) => 
      { 
       char keyhead = item.WordName[0]; 
       return keyhead.ToString().ToLower(); 
      }; 

這件事情是不是真的有據可查的!

相關問題