所选ListBox项的对象返回null

本文关键字:返回 null 对象 ListBox 所选 | 更新日期: 2023-09-27 18:05:38

我有一个ListBox,它显示来自web服务的注释列表。当我运行应用程序时,评论显示良好。现在,我想使用ListBox中所选项目的对象的属性,并获取ID,然后将其分配给字符串值。当我现在运行该应用程序,并单击注释选择它时,我在visualstudio中得到一个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,但在列表框

所选ListBox项的对象返回null

中显示良好

您的代码中有两个问题:

问题1:您正在铸造错误的对象:

在这种情况下;sender就是ListBox,实际上是将ListBox强制转换为Comment对象。这就是它抛出NullReferenceException的原因。

解决方案:

基本上,您必须强制转换SelectedItems,这在SelectionChangedEventArgs实例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);
        }
    }

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

您不应该重新初始化它,它会使所有内容成为null

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

您首先正确地获得了注释,但紧接着您将一个新的Comment分配给同一个变量,因此现在它有一个空的id