I have a ListBox that diplays list of comments from a web service. When I run the app, the comments display fine. Now I want to use a property of the object of a selected item of the ListBox, and get the ID out and then assign it to a string value. When I now run the app, and click on a comment to select it, I get a Null exception in visual studio. I then set a break point on the line of code and try to get the value on mouse over, it returned all the object quite all right but they were all null. code snippets below:
<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>
and in the code behind:
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);
}
... The CommentList, that the lbxComments binds to is a type of 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; }
}
so now I don't know why it's returning null when I try to get the selected item object, but displaying fine when in the list box
There are 2 problems in your code:
Problem 1: You are casting wrong object:
In this event; sender
is the ListBox
, you are actually casting a ListBox
into a Comment
object. Thats why its throwing a NullReferenceException
.
Solution:
Basically you have to cast SelectedItems
, which are available in SelectionChangedEventArgs
instance e
. It has a property AddedItems
, it contains the list of selected items.
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);
}
}
Problem 2: You are re initializing comment
:
You should not re initialize it, it makes everything null
.