1

我正在尝试将对象添加到 ObservableCollection。正如这个网站上的几个问题所述,我什至尝试在添加项目之前实例化集合。但是,我仍然收到错误消息。这是我的观察集:

//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

1 回答 1

1

好的,我终于得到了解决方案!这个问题本身就有点晦涩难懂。问题是,早些时候,我将 RadJumplist 绑定到 aList<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();
            };

这件事没有很好的记录!

于 2012-09-15T09:47:18.340 回答