我需要使用listview
array
我的XAML是:
<Page.Resources>
<DataTemplate x:Key="IconTextDataTemplate">
<StackPanel Orientation="Horizontal" Width="220" Height="60">
<Border Background="#66727272" Width="40" Height="40" Margin="10">
<Image Source="/SampleImage.png" Height="32" Width="32" Stretch="UniformToFill"/>
</Border>
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
<TextBlock x:Name="Name" Margin="10,0,0,0" Width="170" Height="20" TextTrimming="WordEllipsis" Loaded="Name_Loaded"/>
<TextBlock x:Name="Description" Margin="10,0,0,0" Width="170" Height="20" TextTrimming="WordEllipsis" Loaded="Description_Loaded"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</Page.Resources>
<ListView x:Name="IconTextGrid" Height="400" ItemTemplate="{StaticResource IconTextDataTemplate}" Grid.Row="4" Margin="40,40,40,10" HorizontalAlignment="Stretch">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsWrapGrid MaximumRowsOrColumns="8"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
和C#代码:
private async void SearchBox_QuerySubmitted(SearchBox sender, SearchBoxQuerySubmittedEventArgs args)
{
foreach (var item in results)
{
IconTextGrid.Items.Add("");
nameStr = item.FirstName + " " + item.LastName;
descriptionStr = item.Email;
}
}
public T FindElementByName<T>(DependencyObject element, string sChildName) where T : FrameworkElement
{
T childElement = null;
var nChildCount = VisualTreeHelper.GetChildrenCount(element);
for (int i = 0; i < nChildCount; i++)
{
FrameworkElement child = VisualTreeHelper.GetChild(element, i) as FrameworkElement;
if (child == null)
continue;
if (child is T && child.Name.Equals(sChildName))
{
childElement = (T)child;
break;
}
childElement = FindElementByName<T>(child, sChildName);
if (childElement != null)
break;
}
return childElement;
}
private void Name_Loaded(object sender, RoutedEventArgs e)
{
TextBlock Name = FindElementByName<TextBlock>(this, "Name");
Name.Text = nameStr;
}
private void Description_Loaded(object sender, RoutedEventArgs e)
{
TextBlock Description = FindElementByName<TextBlock>(this, "Description");
Description.Text = descriptionStr;
}
此代码应生成包含一些项目(在数组大小中)的列表视图,并将每个项目中的数据放入数组中。 问题是,只有在一个项目中,我才能看到数组中的数据,而其他项目则没有。 希望得到帮助,谢谢
答案 0 :(得分:1)
您的代码失败的原因至少有一个:
foreach (var item in results)
{
IconTextGrid.Items.Add("");
nameStr = item.FirstName + " " + item.LastName;
descriptionStr = item.Email;
}
您似乎将nameStr
和descriptionStr
存储到字段中。但是您只存储结果中的最后一项。
好消息是,您可以一次性简化和修复代码。使用
foreach (var item in results)
{
var nameStr = item.FirstName + " " + item.LastName;
var descriptionStr = item.Email;
IconTextGrid.Items.Add(new { Name = nameStr, Description = descriptionStr });
}
这将为Name
元素创建具有Description
和ListView
属性的匿名类型。然后您可以使用:
<DataTemplate x:Key="IconTextDataTemplate">
<StackPanel ...>
<Image .../>
</Border>
<StackPanel ...>
<TextBlock Text="{Binding Name}" Margin="10,0,0,0" Width="170" Height="20" TextTrimming="WordEllipsis"/>
<TextBlock Text="{Binding Description} Margin="10,0,0,0" Width="170" Height="20" TextTrimming="WordEllipsis"/>
</StackPanel>
</StackPanel>
</DataTemplate>
并移除您的FindElementByName
,Name_Loaded
和Description_Loaded
方法。