需要指导如何使用不同的项目制作列表,将其绑定到ListView

本文关键字:列表 ListView 绑定 项目 何使用 | 更新日期: 2023-09-27 18:18:34

我需要做一个ListView,其中每一行将有一个图像,然后一个文本,然后另一个图像。

既然要使GridView,我必须将其DataContext绑定到一个列表与所有的图像,他们的工作(几乎)相同的方式,我想我需要使另一个列表与图像和文本

格式为:"商店Logo。促进文本。"信用卡标志",所有这些数据都来自一个API。

我已经把所有的图片保存在不同的文件夹中("Stores"answers"PaymentMethods"),我得到了像

这样的文本
myTextBlock.text = item.promotion;

现在我的问题是:

1)有可能用所有这些数据制作一个列表吗?如何?(或者我要去哪里找它)

2)一旦我有了列表,通过绑定它,我怎么能确保它会尊重我上面提到的格式?

(我尝试过的东西,而不是有一个ListView,是在运行时创建一切:

public async void getPromos()
    {
        if (Resp.searchResults.Count > 0)
        {
            var selected = from promo in Resp.searchResults
                           where (promo.store_id != null || promo.method_id != null)
                           select new
                           {
                               store = promo.store_id,
                               medio = promo.method_id,
                               endDay = promo.to,
                               promocion = promo.desc
                           };
            foreach (var item in selected)
            {
                Debug.WriteLine(item.store);
                await showPromos(item.store, item.medio, item.endDay, item.promocion);
            }
        }
    }
    async Task showPromos(string store, string medio, string endDay, string promocion)
    {
        Debug.WriteLine(store);
        Debug.WriteLine("medio" + medio);
        StorageFolder folder1 = await KnownFolders.PicturesLibrary.GetFolderAsync("UnicenterMediosFolder");
        StorageFolder folder2 = await KnownFolders.PicturesLibrary.GetFolderAsync("UnicenterStores");
        if (store != null)
        {
            StorageFile file = await folder2.GetFileAsync(store + ".png");
            ImageProperties properties = await file.Properties.GetImagePropertiesAsync();
            if (properties.Width > 0)
            {
                var bitmap = new WriteableBitmap((int)properties.Width, (int)properties.Height);
                bitmap.SetValue(NameProperty, (string)properties.Title);
                Debug.WriteLine(bitmap.PixelWidth);
                using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
                {
                    bitmap.SetSource(stream);
                }
                Color customColor = ColorHelper.FromArgb(213, 213, 213, 213);
                StackPanel casa = new StackPanel();
                casa.Orientation = Orientation.Horizontal;
                casa.Background = new SolidColorBrush(customColor);
                casa.Height = 70;
                Image promoImage = new Image();
                promoImage.Source = bitmap;
                promoImage.Width = 70;
                promoImage.Height = 70;
                TextBlock thePromo = new TextBlock();
                thePromo.Text = promocion;
                thePromo.Foreground = new SolidColorBrush(Colors.Gray);
                casa.Children.Add(promoImage);
                casa.Children.Add(thePromo);
                gridForPromos.Children.Add(casa);
                Debug.WriteLine("aaa" + gridForPromos.Children.Count);
            }
        }
    }

and in my xaml:

<ScrollViewer x:Name="myPromoSpace" Grid.Row="2">
        <Grid x:Name="gridForPromos"/>
    </ScrollViewer>

但是这样做,我得到每个堆栈面板上的前一个,而不是让他们一个在另一个)

你们能给我指一下正确的方向吗?

需要指导如何使用不同的项目制作列表,将其绑定到ListView

在未设置Grid的情况下向Grid添加元素。行和网格。RowDefinitions,所有的子单元格都在同一个网格单元格中。您需要添加RowDefinitions并设置子元素的行:

gridForPromos.RowDefinitions.Add(new RowDefinition(...))
Grid.SetRow(casa, gridForPromos.RowDefinitions.Count);

或替换你的网格与StackPanel:

<StackPanel x:Name="gridForPromos" />

但是我更喜欢为你的Items定义一个DataTemplate。这样,你就可以在XAML中定义项目的视觉外观,并简单地将StackPanel绑定到项目上。这里有一个链接,告诉你如何做:http://www.wpftutorial.net/datatemplates.html