简体   繁体   English

无法将网格绑定到视图模型-网格中没有数据

[英]Cannot bind grid to viewmodel - no data appears in the grid

Please see the XAML below: 请参阅下面的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"
             xmlns:local="clr-namespace:App1"
             x:Class="App1.MainPage">

    <ContentPage.BindingContext>
        <local:PersonViewModel />
    </ContentPage.BindingContext>

    <StackLayout>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="20" />
            </Grid.RowDefinitions>
            <Label Grid.Column="1" Text="First Name" />
            <Label Grid.Column="2" Text="Surname" />
            <Label Grid.Column="3" Text="Date Of Birth" />
        </Grid>
        <ListView x:Name="listView" ItemsSource="{Binding people}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid>
                            <Label Grid.Column="1" Text="{Binding FirstName}" />
                            <Label Grid.Column="2" Text="{Binding Surname}" />
                            <Label Grid.Column="3" Text="{Binding Age}" />
                        </Grid>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ContentPage>

and the View Model: 和视图模型:

public class PersonViewModel
    {
        public ObservableCollection<Person> people = new ObservableCollection<Person>();

        public PersonViewModel()
        {
            people.Add(new Person { Age = 36, FirstName = "Andrew", Surname = "Smith" });
            people.Add(new Person { Age = 65, FirstName = "David", Surname = "White" });
            people.Add(new Person { Age = 39, FirstName = "Bert", Surname = "Edwards" });
        }
    }

Only the column headings display. 仅显示列标题。 Why does the data not display? 为什么数据不显示?

I have spent all afternoon trying to get this to work. 我整个下午都在努力使它工作。 I have read lots of different answers, however I still cannot get any data to display. 我已经阅读了许多不同的答案,但是仍然无法显示任何数据。

You are missing the Grid Column Definitions (if it is the same as the header, so your xaml should look like this: 您缺少“网格列定义”(如果它与标题相同),因此您的xaml应该如下所示:

 <StackLayout>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="* />
                <ColumnDefinition Width="* />
                <ColumnDefinition Width="* />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="20" />
            </Grid.RowDefinitions>
            <Label Grid.Column="1" Text="First Name" />
            <Label Grid.Column="2" Text="Surname" />
            <Label Grid.Column="3" Text="Date Of Birth" />
        </Grid>
        <ListView x:Name="listView" ItemsSource="{Binding people}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid>
                             <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="* />
                                <ColumnDefinition Width="* />
                                <ColumnDefinition Width="* />
                            </Grid.ColumnDefinitions>
                            <Label Grid.Column="1" Text="{Binding FirstName}" />
                            <Label Grid.Column="2" Text="{Binding Surname}" />
                            <Label Grid.Column="3" Text="{Binding Age}" />
                        </Grid>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM