所选 ListBox 项的对象返回 null
Object of selected ListBox item returning null
我有一个 ListBox,它显示来自 Web 服务的评论列表。当我 运行 应用程序时,评论显示正常。现在我想使用 属性 列表框的 selected 项目的对象,并获取 ID,然后将其分配给字符串值。当我现在 运行 应用程序,并单击对 select 的评论时,我在 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; }
}
所以现在我不知道为什么当我尝试获取 selected 项目对象时返回 null,但在列表框中显示正常
Comment comment = ((sender as FrameworkElement).DataContext) as Comment;
comment = new Comment(); // <- reassignment here
您正确地首先获得了评论,但之后您将新的 Comment
分配给同一个变量,所以现在它有一个空的 id
.
你的代码有两个问题:
问题 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
。
我有一个 ListBox,它显示来自 Web 服务的评论列表。当我 运行 应用程序时,评论显示正常。现在我想使用 属性 列表框的 selected 项目的对象,并获取 ID,然后将其分配给字符串值。当我现在 运行 应用程序,并单击对 select 的评论时,我在 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; }
}
所以现在我不知道为什么当我尝试获取 selected 项目对象时返回 null,但在列表框中显示正常
Comment comment = ((sender as FrameworkElement).DataContext) as Comment;
comment = new Comment(); // <- reassignment here
您正确地首先获得了评论,但之后您将新的 Comment
分配给同一个变量,所以现在它有一个空的 id
.
你的代码有两个问题:
问题 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
。