1

我需要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;
    }

这段代码应该生成一个包含一些项目(在数组大小中)的列表视图,并将数组中单元格中的数据放入每个项目中。问题是我只在一个项目中看到数组中的数据,而在其他项目中什么也没有。求帮助,谢谢

4

1 回答 1

1

您的代码失败至少有一个原因:

    foreach (var item in results)
    {
        IconTextGrid.Items.Add("");
        nameStr = item.FirstName + " " + item.LastName;
        descriptionStr = item.Email;
    }

您似乎正在存储nameStrdescriptionStr进入字段。但是您只将最后一项存储在结果中。

好消息是您可以一次性简化和修复您的代码。使用

    foreach (var item in results)
    {
        var nameStr = item.FirstName + " " + item.LastName;
        var descriptionStr = item.Email;
        IconTextGrid.Items.Add(new { Name = nameStr, Description = descriptionStr });
    }

这将为元素创建具有NameDescription属性的匿名类型。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_LoadedDescription_Loaded方法。

于 2015-02-12T18:50:31.143 回答