Reputation: 187
I dynamically populate a listview with XAML like below
<ListView>
<ListView.ItemTemplate>
<DataTemplate x:Name="templateTrending" >
<StackPanel Orientation="Horizontal">
<TextBlock x:Name="questionBlock" Text="{Binding Path=ques}" Margin="20" />
<TextBlock x:Name="categoryBlock" Text="{Binding Path=categ}" Margin="20"/>
<TextBlock x:Name="userBlock" Text="{Binding Path=user}" Margin="20"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Now in the listviewselection changed method I need to get the values of the textblocks and pass to next XAML page.I tried Jerry Nixon's method but am not able to figure it out.Then I tried wpf method that too didn't work out.So how to achieve this.And below is the code how I assigned the value to the list view
public class Product
{
public string Question { get; set; }
public string Category { get; set; }
public string User { get; set; }
}
And adding it the list view as below
ParseQuery<ParseObject> query = ParseObject.GetQuery("Questions").Include("user");
IEnumerable<ParseObject> res = await query.FindAsync();
List<Product> list = new List<Product>();
foreach (var i in res)
{
var u = i.Get<ParseUser>("user").Username;
var q = i.Get<string>("question");
var c = i.Get<string>("category");
list.Add(new Product
{
Question = q,
Category = c,
User = u,
});
}
listTrending.ItemsSource = list;
Am getting the data from parse.com backend.And my selected item code as below
private void listTrending_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string Qpass = listTrending.SelectedItem.ToString();
this.Frame.Navigate(typeof(WallScreen), Qpass);
}
And the code under onnvaigated to method as below
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (e.Parameter != null)
{
textQuestion.Text = e.Parameter.ToString();
}
else
{
textQuestion.Text = "";
}
}
Upvotes: 0
Views: 1757
Reputation: 2891
You need to post the code for your selection changed event handler. I am not sure I uderstand what you are trying to do because if all you are trying to do is get the contents of the selected item it is as easy as this:
private void myListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if(e.AddedItems.Count > 0)
{
Product p = e.AddedItems[0] as Product;
}
}
Upvotes: 0
Reputation: 31724
I think you're looking at it the wrong way. If you are trying to get the values of a selected item and the values are displayed in UI using bindings - you can extract these values from the binding source which in your case is the listTrending.SelectedItem
- you just need to cast it to Product
.
Upvotes: 1