所选ListBox项的对象返回null

时间:2015-07-31 08:03:01

标签: c# windows-phone-8 listbox

我有一个ListBox,它可以显示来自Web服务的注释列表。当我运行应用程序时,评论显示正常。现在我想使用ListBox的选定项的对象的属性,并获取ID,然后将其分配给字符串值。当我现在运行应用程序,并单击注释以选择它时,我在visual studio中得到一个Null异常。然后我在代码行上设置一个断点并尝试获取鼠标上的值,它完全返回所有对象,但它们都是null。 代码片段如下:

<ListBox x:Name="lbxComments" Margin="0,0,-12,0" 
                 ItemsSource="{Binding CommentList,Mode=TwoWay}"
                 SelectionChanged="lbxComments_SelectionChanged"
                 >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="auto"/>
                            <ColumnDefinition Width="480"/>

                        </Grid.ColumnDefinitions>

                        <Grid.RowDefinitions>
                            <RowDefinition Height="auto"/>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>

                        <Image Stretch="UniformToFill" Height="50"  Width="50" Source="{Binding profile_pic}"   Margin="8" HorizontalAlignment="Left" VerticalAlignment="Top"/>
                        <Border Grid.ColumnSpan="2" Grid.Row="0" HorizontalAlignment="Stretch" BorderBrush="Black" BorderThickness="0,0,0,0.5"/>
                        <StackPanel   Grid.Column="1" >
                            <TextBlock  VerticalAlignment="Center" HorizontalAlignment="Left"  FontSize="20" Text="{Binding comment}" TextWrapping="Wrap" TextTrimming="WordEllipsis" Foreground="White" />
                            <StackPanel Orientation="Horizontal" Margin="0,0,0,5">
                                <TextBlock  VerticalAlignment="Center" HorizontalAlignment="Left"  FontSize="16" Text="{Binding fname}" Foreground="White"/>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock  VerticalAlignment="Center" HorizontalAlignment="Left"  FontSize="16" Text="{Binding lname}" Margin="10 0 0 0" Foreground="White"/>

                                        <StackPanel Orientation="Horizontal">
                                            <TextBlock Text="Replies"/>
                                            <TextBlock Text="("/>
                                            <TextBlock Text="{Binding totalreplies}"/>
                                            <TextBlock Text=")" />
                                        </StackPanel>

                                </StackPanel>
                            </StackPanel>
                        </StackPanel>



                        <TextBlock x:Name="replyTextBox" Grid.Row="2" Margin="50,0,0,0" Visibility="Collapsed" />

                    </Grid>

                </DataTemplate>

            </ListBox.ItemTemplate>
        </ListBox>

并在后面的代码中:

private void lbxComments_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        string selectedCommentId;
        Comment comment = ((sender as FrameworkElement).DataContext) as Comment;
        comment = new Comment();
        if (comment.id != null)
        {
            selectedCommentId = comment.id;
            repliesViewModel.SetAddress(selectedCommentId);
        }

... lbxComments绑定的CommentList是一种Comment

public class Comment
{
  public string fname { get; set; }
  public string lname { get; set; }
  public string profile_pic { get; set; }
  public string id { get; set; }
  public string username { get; set; }
  public string comment { get; set; }
  public string totalreplies { get; set; }
}

所以现在我不知道为什么当我尝试获取所选项目对象时它返回null,但在列表框中显示正常

2 个答案:

答案 0 :(得分:0)

Comment comment = ((sender as FrameworkElement).DataContext) as Comment;
comment = new Comment(); // <- reassignment here

您正在首先正确收到评论,但在此之后您将新的Comment分配给同一个变量,因此它现在为空id

答案 1 :(得分:0)

您的代码中存在两个问题:

问题1:您输错了对象:

在这种情况下; senderListBox,您实际上是将ListBox投射到Comment对象中。这就是为什么它抛出一个NullReferenceException

<强>解决方案:

基本上,您必须投放SelectedItems实例e中提供的AddedItems。它有一个属性private void lbxComments_SelectionChanged(object sender, SelectionChangedEventArgs e) { //get all selected items and cast them into 'Comment' foreach (Comment comment in e.AddedItems) { string selectedCommentId; if (comment.id != null) { selectedCommentId = comment.id; repliesViewModel.SetAddress(selectedCommentId); } } ,它包含所选项目的列表。

comment

问题2:您正在重新初始化null

你不应该重新初始化它,它会使所有内容>>> sorted(2*range(5)) [0, 0, 1, 1, 2, 2, 3, 3, 4, 4]