XAML/MVVMx 将自定义对象数组绑定到网格时出现问题

XAML/MVVMx Problems with binding custom Array of object to a Grid

我尝试将一个带有自定义文本的对象绑定到我的 GridView 上进行测试,但无法真正得到解决方案。

我已经尝试使用 Grid.BindingContext 让它滚动,但它不起作用。

有人可以帮我吗?

<Grid>
<Grid.BindingContext>
<x:Array Type="{x:Type clients:MinRepresentation}<clients:MinRepresentation Id="123456789" PlannedStartTime="01-01-2019" PlannedEndTime="01-12-2019"  />
</x:Array>
</Grid.BindingContext>


<StackLayout Orientation="Horizontal" Padding="0,10,0,5" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="3" BackgroundColor="#CC3F6E3F">
<Image Source="{helpers:ImageResource catkinApp.UI.Images.state_new.png}" Margin="10,0,0,0" />
<Label Text="{Binding ID, StringFormat='ID: [{0}]'}" FontSize="Small" Margin="5,0,10,0" FontAttributes="Bold" TextColor="#FFFFFF"/>
<Label HorizontalTextAlignment="End" Text="(In Bearbeitung)" FontSize="Small" FontAttributes="Bold" TextColor="#FFFFFF"/>
</StackLayout>
</Grid>

根据你的描述,你想为Grid.BindingContext绑定自定义对象,你不需要使用Array,你只需要这样做:

 public class model1
{
    public string str1 { get; set; }
    public string str2 { get; set; }
}

 <Grid>
        <Grid.BindingContext>
            <local:model1 str1="aaaa" str2="bbbb"></local:model1>
        </Grid.BindingContext>
        <StackLayout Orientation="Vertical">
            <Label Text="this is test, please take a look!" />
            <Label
                Margin="5,0,10,0"
                FontAttributes="Bold"
                FontSize="Small"
                Text="{Binding str1}"
                TextColor="#FFFFFF" />
            <Label
                FontAttributes="Bold"
                FontSize="Small"
                Text="{Binding str2}"
                TextColor="#FFFFFF" />

        </StackLayout>
    </Grid>

这里是MSDN的文章,你可以看看: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/string-formatting