【问题标题】:Xamarin binding is not working to listviewXamarin 绑定不适用于列表视图
【发布时间】:2020-11-18 07:54:43
【问题描述】:

我尝试将一些测试数据与我的 xamarin 表单页面 usint ListView 绑定以使其可滚动。但是,由于某种原因,我无法正确地将数据与 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"
            }
        };
    }
}

我有以下带有列表视图的 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 代码正确绑定数据。谁能帮助我做错了什么?

谢谢!

【问题讨论】:

  • 您需要正确实现INotifyProperyChanged接口,否则绑定将不会收到ObjectList有新值的通知(创建列表)
  • 或者,您可以在初始化数据之后 分配 BindingContext

标签: c# xamarin mobile xamarin.forms


【解决方案1】:

根据我的测试,你需要把Grid放在DataTemplate的ViewCell里面。就像 Jason 说的那样,在初始化数据后输入 BindingContext

Xaml:

 <ContentPage.Content>

    <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
                x:Name="listTransactions"
                Grid.Row="1"
                Grid.Column="0"
                Grid.ColumnSpan="2"
                ItemsSource="{Binding ObjectList}"
                VerticalScrollBarVisibility="Default">
                <ListView.Header>
                    <Grid>
                        <Grid.RowDefinitions>
                            <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>
                        <ViewCell>

                            <Grid>
                                <Grid.RowDefinitions>
                                    <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>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </Grid>
    </StackLayout>

</ContentPage.Content>

代码背后:

 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; }
}

截图:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-17
    • 2015-04-22
    • 1970-01-01
    • 2012-09-01
    • 2017-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多