C# Xamarin绑定不适用于listview

C# Xamarin绑定不适用于listview,c#,xamarin,mobile,xamarin.forms,C#,Xamarin,Mobile,Xamarin.forms,我尝试使用ListView将一些测试数据与我的xamarin表单页面绑定,以使其可滚动。但是,由于某些原因,我无法将数据与ListView正确绑定。 我有以下ContentPage类: public class transactionTest { public string Type { get; set; } public decimal Amount { get; set; } public string User { get; set; } public D

我尝试使用ListView将一些测试数据与我的xamarin表单页面绑定,以使其可滚动。但是,由于某些原因,我无法将数据与ListView正确绑定。 我有以下ContentPage类:

public class transactionTest
{
    public string Type { get; set; }
    public decimal Amount { get; set; }
    public string User { get; set; }
    public DateTime Date { get; set; }
}

[DesignTimeVisible(false)]
public partial class MainPageUser : ContentPage
{
    public ObservableCollection<transactionTest> ObjectList { get; set; }

    public MainPageUser()
    {
        InitializeComponent();
        BindingContext = this;

        ObjectList = new ObservableCollection<transactionTest>()
        {
            new transactionTest()
            {
                Amount = 231,
                Date = DateTime.Parse("2020-06-12 15:22"),
                Type = "Send",
                User = "Test"
            },
            new transactionTest()
            {
                Amount = 112,
                Date = DateTime.Parse("2020-06-13 12:22"),
                Type = "Send",
                User = "Test"
            },
            new transactionTest()
            {
                Amount = 131,
                Date = DateTime.Parse("2020-06-11 13:22"),
                Type = "Receive",
                User = "Test"
            },
            new transactionTest()
            {
                Amount = 225,
                Date = DateTime.Parse("2020-06-14 10:22"),
                Type = "Send",
                User = "Test"
            },
            new transactionTest()
            {
                Amount = 201,
                Date = DateTime.Parse("2020-06-15 16:22"),
                Type = "Receive",
                User = "Test"
            }
        };
    }
}
公共类事务测试
{
公共字符串类型{get;set;}
公共十进制数{get;set;}
公共字符串用户{get;set;}
公共日期时间日期{get;set;}
}
[设计时间可见(错误)]
公共部分类MainPageUser:ContentPage
{
公共ObservableCollection对象列表{get;set;}
公共主页用户()
{
初始化组件();
BindingContext=这个;
ObjectList=新的ObservableCollection()
{
新transactionTest()
{
金额=231,
Date=DateTime.Parse(“2020-06-12 15:22”),
Type=“发送”,
User=“测试”
},
新transactionTest()
{
金额=112,
Date=DateTime.Parse(“2020-06-13 12:22”),
Type=“发送”,
User=“测试”
},
新transactionTest()
{
金额=131,
Date=DateTime.Parse(“2020-06-11 13:22”),
Type=“接收”,
User=“测试”
},
新transactionTest()
{
金额=225,
Date=DateTime.Parse(“2020-06-1410:22”),
Type=“发送”,
User=“测试”
},
新transactionTest()
{
金额=201,
Date=DateTime.Parse(“2020-06-15 16:22”),
Type=“接收”,
User=“测试”
}
};
}
}
我在listview中有以下XAML代码:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:d="http://xamarin.com/schemas/2014/forms/design"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         mc:Ignorable="d"
         x:Class="MyNameSpace.ViewPages.MainPageUser">
<StackLayout>
    <Grid VerticalOptions="CenterAndExpand">
        <Grid.RowDefinitions>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="2*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <ListView Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" x:Name="listTransactions" VerticalScrollBarVisibility="Default" ItemsSource="{Binding ObjectList}">
            <ListView.Header>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition></RowDefinition>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <Label Grid.Row="0" Grid.Column="0" HorizontalTextAlignment="Center">Type</Label>
                    <Label Grid.Row="0" Grid.Column="1" HorizontalTextAlignment="Center">Amount</Label>
                    <Label Grid.Row="0" Grid.Column="2" HorizontalTextAlignment="Center">User</Label>
                    <Label Grid.Row="0" Grid.Column="3" HorizontalTextAlignment="Center">Date</Label>
                </Grid>
            </ListView.Header>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition></RowDefinition>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <Label Grid.Row="0" Grid.Column="0" HorizontalTextAlignment="Center" Text="{Binding Type}"/>
                        <Label Grid.Row="0" Grid.Column="1" HorizontalTextAlignment="Center" Text="{Binding Amount}"/>
                        <Label Grid.Row="0" Grid.Column="2" HorizontalTextAlignment="Center" Text="{Binding User}"/>
                        <Label Grid.Row="0" Grid.Column="3" HorizontalTextAlignment="Center" Text="{Binding Date}"/>
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</StackLayout>

类型
数量
使用者
日期
我无法使用XAML代码正确绑定数据。谁能帮我一下我做错了什么


谢谢

根据我的测试,您需要将网格放在DataTemplate的
ViewCell
中。正如Jason所说,在初始化数据后,将
BindingContext
放入

Xaml:


类型
数量
使用者
日期
代码隐藏:

 public partial class Page18 : ContentPage
{
    public ObservableCollection<transactionTest> ObjectList { get; set; }
    public Page18()
    {
        InitializeComponent();
       
        ObjectList = new ObservableCollection<transactionTest>()
        {
            new transactionTest (){ Amount = 231, Date = DateTime.Parse("2020-06-12 15:22"), Type = "Send", User = "Test"},
            new transactionTest() { Amount = 112, Date = DateTime.Parse("2020-06-13 12:22"), Type = "Send",  User = "Test" },
            new transactionTest() { Amount = 131, Date = DateTime.Parse("2020-06-11 13:22"), Type = "Receive", User = "Test"},
            new transactionTest() { Amount = 225, Date = DateTime.Parse("2020-06-14 10:22"), Type = "Send", User = "Test"},
            new transactionTest() { Amount = 201, Date = DateTime.Parse("2020-06-15 16:22"), Type = "Receive",User = "Test"}
        };
        this.BindingContext = this;


    }
}
public class transactionTest
{
    public string Type { get; set; }
    public int Amount { get; set; }
    public string User { get; set; }
    public DateTime Date { get; set; }
}
公共部分类第18页:内容页
{
公共ObservableCollection对象列表{get;set;}
公共页18()
{
初始化组件();
ObjectList=新的ObservableCollection()
{
newtransactiontest(){Amount=231,Date=DateTime.Parse(“2020-06-12 15:22”),Type=“Send”,User=“Test”},
new transactionTest(){Amount=112,Date=DateTime.Parse(“2020-06-13 12:22”),Type=“Send”,User=“Test”},
new transactionTest(){Amount=131,Date=DateTime.Parse(“2020-06-11 13:22”),Type=“Receive”,User=“Test”},
new transactionTest(){Amount=225,Date=DateTime.Parse(“2020-06-14 10:22”),Type=“Send”,User=“Test”},
newtransactiontest(){Amount=201,Date=DateTime.Parse(“2020-06-15 16:22”),Type=“Receive”,User=“Test”}
};
this.BindingContext=this;
}
}
公共类事务测试
{
公共字符串类型{get;set;}
公共整数金额{get;set;}
公共字符串用户{get;set;}
公共日期时间日期{get;set;}
}
截图:


您需要实现
InotifyPropertyChange
 public partial class Page18 : ContentPage
{
    public ObservableCollection<transactionTest> ObjectList { get; set; }
    public Page18()
    {
        InitializeComponent();
       
        ObjectList = new ObservableCollection<transactionTest>()
        {
            new transactionTest (){ Amount = 231, Date = DateTime.Parse("2020-06-12 15:22"), Type = "Send", User = "Test"},
            new transactionTest() { Amount = 112, Date = DateTime.Parse("2020-06-13 12:22"), Type = "Send",  User = "Test" },
            new transactionTest() { Amount = 131, Date = DateTime.Parse("2020-06-11 13:22"), Type = "Receive", User = "Test"},
            new transactionTest() { Amount = 225, Date = DateTime.Parse("2020-06-14 10:22"), Type = "Send", User = "Test"},
            new transactionTest() { Amount = 201, Date = DateTime.Parse("2020-06-15 16:22"), Type = "Receive",User = "Test"}
        };
        this.BindingContext = this;


    }
}
public class transactionTest
{
    public string Type { get; set; }
    public int Amount { get; set; }
    public string User { get; set; }
    public DateTime Date { get; set; }
}