Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have a listbox and a template that contains a grid and two textblocks(ID , Description). I have used this template to view the results of some query on the access database. I have binded the textblocks to columns of datatable.

However, how can i get the value of just the description textblock which is selected, as string?
Posted

1 solution

If the list box is multi-selectable, you can use the property SelectedItems, otherwise you can use the property SelectedItem, SelectedValue or SelectedIndex. You will come to the item reference, which can actually be of any type, not just string, so the compile-time type of the returned object is System.Object; you can check up its runtime type, but from your design your know what is it. It should be either TextBlock or some UI elements with to child text blocks (I don't know you exact design as you did not share any code, but you should know it). This way, you come to the instance of TextBlock, so you can extract the text using the property TextBlock.Text. That's all.

Please see:
http://msdn.microsoft.com/en-us/library/system.windows.controls.listbox.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.selector.selectedindex.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.selector.selecteditem.aspx[^]
http://msdn.microsoft.com/en-us/library/system.windows.controls.listbox.selecteditems.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.selector.selectedvalue.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.controls.textblock.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.controls.textblock.text.aspx[^].

—SA
 
Share this answer
 
Comments
missak boyajian 17-Jul-13 15:45pm    
The object is Datarow that contains two textblocks. How can I get the value of the textblocks?
String ItemName = ListProducts.SelectedValue.ToString(); is returning System.Data.DataRowView.
Sergey Alexandrovich Kryukov 17-Jul-13 17:45pm    
Whatever. Anything you can put in a list item should be found; and you should type cast the value to this type. As simple as that.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900