C# 获取windows 8应用商店应用程序中Listview itemtemplate中textblocks的值
我使用XAML动态填充listview,如下所示C# 获取windows 8应用商店应用程序中Listview itemtemplate中textblocks的值,c#,windows-8,windows-store-apps,winrt-xaml,C#,Windows 8,Windows Store Apps,Winrt Xaml,我使用XAML动态填充listview,如下所示 <ListView> <ListView.ItemTemplate> <DataTemplate x:Name="templateTrending" > <StackPanel Orientation="Horizontal"> <TextBlock x:Name="questionBlock" Text="{Bi
<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>
并将其添加到列表视图中,如下所示
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;
下面的代码引用到方法
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (e.Parameter != null)
{
textQuestion.Text = e.Parameter.ToString();
}
else
{
textQuestion.Text = "";
}
}
我想你看错了。如果您试图获取所选项目的值,并且这些值使用绑定显示在UI中-您可以从绑定源中提取这些值,在您的情况下是
列表趋势。SelectedItem
-您只需将其强制转换到产品,您需要发布所选更改事件的代码处理程序。我不确定我是否能理解您试图做的事情,因为如果您试图做的只是获取所选项目的内容,那么就很容易做到:
private void myListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if(e.AddedItems.Count > 0)
{
Product p = e.AddedItems[0] as Product;
}
}
是的,我正在尝试获取listview所选项目的值。到目前为止,我获得的输出为myappname.Screens.wallscreen.Product。因此,对于上述代码,我希望将listview所选项目中questionBlock的值传递给textQuestion。我希望获得每个变量中每个textblock的值,然后在下一个屏幕中将值传递给Textblocks。但是我得到的输出是myappname.Screens.wallscreen+ProductReplacestring qPass=listTrending.SelectedItem.ToString()代码>类似于字符串user=((产品)列表趋势。SelectedItem)。user
。另外,请确保属性名以大写字母开头,除非它们的名称需要与某些json对象属性名匹配,因为这是C#中非常强大的行业标准命名约定。或者,重写ToString()产品上的方法
键入以返回所需内容,然后列表趋势。选择EdItem.ToString()
应返回该值。还要确保处理listrending.SelectedItem==null
cases.it WORKED!!!!呸!!谢谢菲利普·斯卡昆,非常感谢!为了这个简单的代码,我绞尽脑汁了好几个小时!我得到的p值是myappname.Screens.wallscreen+Product尝试将上面的代码更改为“string qPass=((Product)(listrending.SelectedItem)).ques;”;谢谢Jon。菲利普·斯卡昆和你帮了我。
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (e.Parameter != null)
{
textQuestion.Text = e.Parameter.ToString();
}
else
{
textQuestion.Text = "";
}
}
private void myListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if(e.AddedItems.Count > 0)
{
Product p = e.AddedItems[0] as Product;
}
}