Xamarin将数据绑定到ListView中的网格组件

时间:2017-05-28 03:44:54

标签: listview xamarin xamarin.forms grid

我试图绑定数据,但是列表视图没有显示我调试它的任何内容,我看到项目是正确的,但列表视图没有显示任何内容。如何将我的列表绑定到列表视图,我搜索了很多,但我找不到任何解决方案

ResultPage.xaml.cs

using System;
using System.Collections.Generic;
using FindDiscount.Models;
using Xamarin.Forms;
using System.Collections.ObjectModel;

namespace FindDiscount {
    public partial class ResultPage : ContentPage {
        public ObservableCollection<resultModel> products;

        public ResultPage(int distance, string productName) {
            InitializeComponent();
            initData();



        }


        public ResultPage() {
            InitializeComponent();

        }

        void initData(){

            products = new ObservableCollection<resultModel>();
            products.Add(new resultModel{
                productName="Samsung Note 4",
                companyName="Samsung",
                productPrice=1000,
                discountRate=15,
                imageURL="hamburger.png"
            });
            products.Add(new resultModel {
                productName = "iphone 7",
                companyName = "Apple",
                productPrice = 3000,
                discountRate = 45,
                imageURL = "hamburger.png"
            });
            products.Add(new resultModel {
                productName = "Windows Phone ",
                companyName = "Microsoft",
                productPrice = 1000,
                discountRate = 15,
                imageURL = "hamburger.png"
            });
            ProductsListView.ItemsSource = products;
        }
    }
}

ResultPage.xaml

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage 
    xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    x:Class="FindDiscount.ResultPage">
    <ContentPage.Content>

    <ListView x:Name="ProductsListView" HasUnevenRows="true" ItemsSource="{Binding products}">
      <ListView.ItemTemplate>
         <DataTemplate>
            <ViewCell>
              <ViewCell.View>
                      <Grid>
                             <Grid.RowDefinitions>
                                <RowDefinition Height="*"/>
                                <RowDefinition Height="*"/>
                                <RowDefinition Height="*"/>
                                <RowDefinition Height="*"/>
                             </Grid.RowDefinitions>

                             <Grid.ColumnDefinitions>
                             <ColumnDefinition  Width="*"/>
                              <ColumnDefinition  Width="*"/>
                            </Grid.ColumnDefinitions>

                             <Image  Grid.Row="0" Grid.Column="0" Source="{Binding imageURL}" />       

                             <Label Grid.Row="0" Grid.Column="1" Text="{Binding productName}" TextColor="Black"  />

                             <Label  Grid.Row="1" Grid.Column="1" Text="{Binding companyName}" TextColor="Black" />

                             <Label Grid.Row="2" Grid.Column="0" Text="{Binding productPrice}" TextColor="Black"  />

                             <Label Grid.Row="2" Grid.Column="1"  Text="{Binding discountRate}" TextColor="Black" />

                             <Button Grid.Row="3" Grid.Column="1" Text="Locaiton" TextColor="Black" />

                        </Grid>
                     </ViewCell.View>
               </ViewCell>               
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>


    </ContentPage.Content>
</ContentPage>

1 个答案:

答案 0 :(得分:0)

我认为你应该写点像

   public ObservableCollection<resultModel> products {get; set;}

并在INotifyPropertyChanged

中实施resultModel