xamarin 在代码隐藏的 ListView 中形成绑定嵌套列表

xamarin forms binding nested Lists in ListView in code behind

我正在尝试将 Foo 绑定到此列表视图。我曾尝试使用自定义转换器,也曾尝试使用嵌套列表视图。这应该在后面的代码中完成吗?完成此任务的最佳方法是什么?

<ListView x:Name="DisplayFoos">
    <ListView.ItemTemplate>
        <DataTemplate>
        <ViewCell>
                <StackLayout Orientation="Vertical" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" Spacing="0" Margin="0">
                    <customControl:CustomFrame CornerRadius="2" BackgroundColor="White" OutlineColor="Gray" Margin="0">
                        <StackLayout Orientation="Horizontal" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">
                            <Label FontSize="Large" Text="{Binding FooDate,StringFormat='{0:M/dd/yyyy}'}" TextColor="White" />
                            <Label FontSize="Large" Text="{Binding NumberOfBars,StringFormat='{0} Vehicles'}"  TextColor="White" />
                        </StackLayout> 
                    </customControl:CustomFrame>
                    <Frame CornerRadius="2" BackgroundColor="White" OutlineColor="Gray" Margin="0">
                    <StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand"  VerticalOptions="FillAndExpand" BackgroundColor="White" Margin="5,5,5,5">
                        <StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
                            <Image Source="{Binding PropOne}" HeightRequest="80" WidthRequest="80" Aspect="AspectFill" />
                        </StackLayout>
                        <StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand">
                            <StackLayout Orientation="Vertical" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
                                <StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" >
                                        <Label Margin="16,0,0,0"  Text="{Binding PropTwo}" FontSize="Micro" FontAttributes="Bold" TextColor="Black"  />
                                    <Label  Text="{Binding PropThree}" FontSize="Micro" FontAttributes="Bold" TextColor="Black"  />
                                        <Label  Text="{Binding PropFour}" FontSize="Micro" FontAttributes="Bold" TextColor="Black"   />
                                        <Label  Text="{Binding PropFive}" FontSize="Micro" FontAttributes="Bold" TextColor="Black"  />
                                    <Label  Text=" - " FontSize="Small" FontAttributes="Bold" TextColor="Black"   />
                                    <Label  Text="{Binding PropSix}" FontSize="Micro" FontAttributes="Bold" TextColor="Black"   />
                                </StackLayout>
                                <Label Margin="5,0,0,0"  Text="Some Text"  FontSize="Small" HorizontalOptions="Start" TextColor="Red"  IsVisible="{Binding PropSeven}"/>
                            </StackLayout>
                        </StackLayout>
                    </StackLayout>
                </Frame>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
public class Foo
{
  public DateTime FooDate {get; set; }
  public int NumberOfBars {get; set; }
  public List<Bar> ListOfBars { get; set; }

}
public class Bar
{
  string PropOne {get set;}
  string PropTwo {get; set;}
  string PropThree {get; set;}
  string PropFour {get; set;}
  string PropFive {get; set; }
  string PropSix  {get; set; }
  bool PropSeven {get; set; }
}
ObservableCollectiont<Foo> listOfFoos = new ObservableCollection<Foo>();
DisplayFoos.ItemSource = listOfFoos;

您可以使用转换器。

public class ListToStringConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is IList<Bar> list)
        {
            return string.Join(", ", list.Select(x => x.PropertyB));
        }
        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

用法:

<DataTemplate>
    <ViewCell>
        <StackLayout>
            <Label Text="{Binding PropertyA}" />
            <Label Text="{Binding ListOfBars, 
                            Converter={StaticResource ListToStringConverter}}" />
        </StackLayout>
    </ViewCell>
</DataTemplate>