Get the values of a SelectedItem in wpf


I have a ListBox in a WPF Page each Item of which consists of two Labels. I have assigned them the names FirstName, LastName:

<ListBox.ItemTemplate> <DataTemplate> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <TextBlock Grid.Column="0" Name="FirstName" Text="{Binding FirstName}" Margin="0,0,10,0"/> <TextBlock Grid.Column="1" Name="LastName" Text="{Binding LastName}" HorizontalAlignment="Left"/> </Grid> </DataTemplate> </ListBox.ItemTemplate>

When the user selects an Item of the Listbox, I want to hold their values separately in order to pass them to another Page. I try to this end in the code behind the code:

ListItemCollection lbi = List1.SelectedItem as ListItemCollection; string first = lbi.FirstListItem.DataContext.ToString(); string last = lbi.LastListItem.DataContext.ToString();

However I get a nullReferenceException at the definition of strings? What is wrong in this way? How can I get the SelectedItem values?

<strong>Update:</strong> The Binding values derive from an SQLAdapter with a SQL Query. See the code behind:

SqlDataAdapter dAdapt1 = new SqlDataAdapter(sqlStr1, cnStr); DataSet dataSet1 = new DataSet(); dAdapt1.Fill(dataSet1); List1.DataContext = dataSet1.Tables[0];


<h2>DataTable and ListBox##</h2>

myDataSet.BookTable. firstColumn and lastColumn.


binding ready



adapter.Fill(myDataSet, "BookTable");


myListBox.DataContext = myDataSet; <blockquote>

binding begin



<ListBox ItemsSource="{Binding Path=BookTable}"

4. Window.Resources>

<DataTemplate x:Key="BookItemTemplate"> <Grid> <TextBlock Text="{Binding Path=first}" Grid.Column="0"/> <TextBlock Text="{Binding Path=last}" Grid.Column="1" />


ItemTemplate ="{StaticResource BookItemTemplate}"/> <blockquote>

<strong>binding end</strong>



DataRowView d1=List1.SelectedItem as DataRowView; string first =d1["first"].tostring(); string last =d1["last"].tostring();


As Andy has already commented, List1.SelectedItem is not a ListItemCollection, so you cannot cast to that type, hence lbi is null.

In the DataTemplate you're binding to properties FirstName and LastName. There must be a class that defines these properties, let's call it Person.

I guess the following, because you haven't shown it. You're binding the ItemSource of the ListBox to a collection of Person objects, provided by some property of the object that is returned by dataSet1.Tables[0] (because you set List1.DataContext to that value).

So what you would have to write to get the selected item is this:

Person lbi = List1.SelectedItems as Person;

and replace Person by whatever that type really is in your application.


  • how to convert ListItemCollection (dropdownlist.items) to a dictionary?
  • The collection has not been initialized
  • Enable text box based on radio button selected
  • Find generic sub-lists within a list
  • Matlab FFT and FFTW
  • jquery - how to CUT LI elements from one UL to another UL
  • Sets in raphaeljs not real groups? Transform order
  • Python list of (node_id, parent_node_id) to JSON
  • Get the results of dis.dis() in a string
  • update query with multiple values
  • How to Get TCanvas DC in Firemonkey?
  • Hough transform in MATLAB
  • DataGrid: Help accessing a control defined in a CellEditingTemplate
  • How to upload all gallery images to a server in Android ?
  • What is the difference between GetComponent ().enabled and .SetActive (false); in unity
  • Change Font Style (bold, Italic, bold italic) in C# [duplicate]
  • JsonConvert.DeserializeObject, Index was outside the bounds of the array
  • actionscript 3.0 transparent image floodfill
  • longest common subsequence: why is this wrong?
  • Multiple for loop iterators to unpack in Python [duplicate]
  • How to notify a specific thread in Java
  • Binding a list with another list with WPF
  • PHP OOXML Libraries? [closed]
  • How to show underscore (shortcut) without holding Alt?
  • HALF_PTR Windows data type
  • Can XOR be expressed using SKI combinators?
  • F#: In which memory area is the continuation stored: stack or heap?
  • Most efficient way to move table rows from one table to another
  • Convert array of 8 bytes to signed long in C++
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Acquiring multiple attributes from .xml file in c#
  • Understanding cpu registers
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • apache spark aggregate function using min value
  • How can I remove ASP.NET Designer.cs files?
  • Recursive/Hierarchical Query Using Postgres
  • Running Map reduces the dimensions of the matrices
  • Sorting a 2D array using the second column C++
  • java string with new operator and a literal