Answered by:
Get Data by e.ClickedItem

Question
-
Hi, I can't operate with e.ClickedItem, my xml code is:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <ListView x:Name="Lista" SelectionMode="None" IsItemClickEnabled="True" ItemClick="Lista_ItemClick"> <ListView.ItemTemplate> <DataTemplate> <StackPanel> <TextBlock Text="{Binding generi}" x:Name="txtshow"/> </StackPanel> </DataTemplate> </ListView.ItemTemplate> </ListView> </Grid>
and my c# code is:
private async void Lista_ItemClick(object sender, ItemClickEventArgs e) { string i = e.ClickedItem.ToString(); var mess = new Windows.UI.Popups.MessageDialog(i); await mess.ShowAsync(); }
and the app returns this:
I mistake to manager the Binding in TextBlock. Please Help me :)
thanks for all.Monday, March 17, 2014 8:33 AM
Answers
-
the lync query returns an auto created class (anonomous class) change the query to this:
var n = from m in jsonList select new Generi { generi = m.GetObject()["Title"].GetString() };
then the casting should workMicrosoft Certified Solutions Developer - Windows Store Apps Using C#
- Marked as answer by Jamles HezModerator Tuesday, April 8, 2014 8:54 AM
Monday, March 24, 2014 10:34 AM
All replies
-
please explain your problem clearly
thanks, prathap If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
Monday, March 17, 2014 12:24 PM -
e.ClickedItem returns an object reference. If you want to access the properties of the selected item, then you have to cast it to the appropriate data type. So if we assume that you assign a List of "YourClassHere" as ItemsSource for the Lista control, then the first line of the ItemClick event handler could look like this:
string i = (e.ClickedItem as YourClassHere).generi;
Monday, March 17, 2014 12:50 PM -
thanks, I have written the class:
class Generi { public string generi { get; set;} }
and I have used the codestring i = (e.ClickedItem as Generi).generi;
But I receved NullReferenceException.Friday, March 21, 2014 3:04 PM -
how are you setting the itemssource of your list now? can show the whole code?
you shouldnt create a new class. becasue your casting to the wrong class you get a nullreferenceexception
Microsoft Certified Solutions Developer - Windows Store Apps Using C#
Friday, March 21, 2014 7:33 PM -
I get data by JSON and insert data from Lista.ItemsSource = n;
n are elements of JSON, and I fill a ListView call "Lista", in my first post you can find the ListView.
var jsonList = JsonArray.Parse(result); // Add: using Windows.Data.Json; var n = from m in jsonList select new { generi = m.GetObject()["Title"].GetString() }; // Display data read from web site Lista.ItemsSource = n;
Sunday, March 23, 2014 9:59 PM -
the lync query returns an auto created class (anonomous class) change the query to this:
var n = from m in jsonList select new Generi { generi = m.GetObject()["Title"].GetString() };
then the casting should workMicrosoft Certified Solutions Developer - Windows Store Apps Using C#
- Marked as answer by Jamles HezModerator Tuesday, April 8, 2014 8:54 AM
Monday, March 24, 2014 10:34 AM -
Work!!! :)
Thanks for all :)Tuesday, April 1, 2014 3:23 PM