我需要将可观察的集合视图绑定到flexlayout(因为我需要自定义外观)。当我将项目绑定到集合视图时,它们的外观与当我在flexlayout中直接使用网格时得到的外观不一样,例如:
这将按预期工作,但没有任何约束。
<FlexLayout Grid.Column="0" Grid.Row="5" Margin="10,15,10,5" HorizontalOptions="FillAndExpand" VerticalOptions="StartAndExpand">
<Grid HeightRequest="20" Margin="0,0,0,6">
<Frame BackgroundColor="#f1f1f1" CornerRadius="5" Padding="2">
<Label Text="some text" Grid.Column="1" Grid.Row="0" TextColor="DarkGray" FontSize="11" />
</Frame>
</Grid>
</FlexLayout>
这里所有内容都可以绑定,但是外观就像一个列表视图:
in code behind: xEspecialidades.ItemsSource = StringCollection;
in xaml:
<FlexLayout Grid.Column="0" Grid.Row="5" Margin="10,15,10,5" HorizontalOptions="FillAndExpand" VerticalOptions="StartAndExpand">
<CollectionView x:Name="xEspecialidades" x:FieldModifier="public static" HorizontalOptions="FillAndExpand" VerticalOptions="StartAndExpand" VerticalScrollBarVisibility="Never" HorizontalScrollBarVisibility="Never">
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid HeightRequest="20" Margin="0,0,0,6">
<Frame BackgroundColor="#f1f1f1" CornerRadius="5" Padding="2">
<Label Text="{Binding .}" Grid.Column="1" Grid.Row="0" TextColor="DarkGray" FontSize="11" />
</Frame>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</FlexLayout>
我已经找到了该线程https://github.com/xamarin/Xamarin.Forms/issues/8234,但无法使其正常工作。 Visual Studio一直在说找不到FlexItemsLayout。有什么方法可以做我想要的吗?
通过使用下面的代码使它起作用:
<FlexLayout
x:Name="xEspecialidades"
BindableLayout.ItemsSource="{Binding especialidades}"
HorizontalOptions="FillAndExpand" VerticalOptions="StartAndExpand">
<BindableLayout.ItemTemplate>
<DataTemplate>
<Grid HeightRequest="20" Margin="0,0,0,6" HorizontalOptions="StartAndExpand">
<Frame BackgroundColor="#f1f1f1" CornerRadius="5" Padding="2" HorizontalOptions="StartAndExpand">
<Label Text="{Binding .}" Grid.Column="1" Grid.Row="0" TextColor="DarkGray" FontSize="11" HorizontalOptions="StartAndExpand" />
</Frame>
</Grid>
</DataTemplate>
</BindableLayout.ItemTemplate>
</FlexLayout>