【问题标题】:Object of selected ListBox item returning null所选 ListBox 项的对象返回 null
【发布时间】:2015-07-31 08:03:00
【问题描述】:

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

【问题讨论】:

    标签: c# windows-phone-8 listbox


    【解决方案1】:

    你的代码有两个问题:

    问题 1:您正在投射错误的对象:

    在这个事件中; senderListBox,您实际上是将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

    【讨论】:

      【解决方案2】:
      Comment comment = ((sender as FrameworkElement).DataContext) as Comment;
      comment = new Comment(); // <- reassignment here
      

      您首先正确地获得了评论,但之后您将新的Comment 分配给同一个变量,所以现在它有一个空的id

      【讨论】:

      • 我这样做是因为 Visual Studio 给了我这个错误:“System.NullReferenceException”类型的异常附加信息:对象引用未设置为对象的实例。故障排除提示:使用 new 关键字创建对象实例。我应该怎么做???
      • 如果你知道可以做什么请回复@gabrielnegut
      猜你喜欢
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 2015-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多