微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在Listview中创建几列?

如何解决如何在Listview中创建几列?

 <ListView x:Name="listview" ItemsSource="{Binding MotorType}"  SelectionMode="None" Margin="0" HeightRequest="220">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <StackLayout Orientation="Horizontal">
                                    <CheckBox HorizontalOptions="Start" Color="Black" CheckedChanged="CheckBox_CheckedChanged"
                                    IsChecked="{Binding IsSelected}"
                                    />
                                    <Label Text="{Binding Name}" TextColor="Gray"></Label>
                                </StackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

我有listview,请说出如何使几列成为空白(我不使用collectionview) 我有这个 enter image description here 我要这个 enter image description here

解决方法

您可以创建一个ExtendedFlexLayout来将您的MotorType ItemSource绑定到它

   public class ExtendedFlexLayout : FlexLayout
{
    public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create(nameof(ItemsSource),typeof(IEnumerable),typeof(ExtendedFlexLayout),propertyChanged: OnItemsSourceChanged);
    public static readonly BindableProperty ItemTemplateProperty = BindableProperty.Create(nameof(ItemTemplate),typeof(DataTemplate),typeof(ExtendedFlexLayout));

    public IEnumerable ItemsSource
    {
        get { return (IEnumerable)GetValue(ItemsSourceProperty); }
        set { SetValue(ItemsSourceProperty,value); }
    }

    public DataTemplate ItemTemplate
    {
        get { return (DataTemplate)GetValue(ItemTemplateProperty); }
        set { SetValue(ItemTemplateProperty,value); }
    }

    static void OnItemsSourceChanged(BindableObject bindable,object oldVal,object newVal)
    {
        IEnumerable newValue = newVal as IEnumerable;
        var layout = (ExtendedFlexLayout)bindable;

        layout.Children.Clear();
        if (newValue != null)
        {
            foreach (var item in newValue)
            {
                layout.Children.Add(layout.CreateChildView(item));
            }
        }
    }

    View CreateChildView(object item)
    {
        ItemTemplate.SetValue(BindableObject.BindingContextProperty,item);
        return (View)ItemTemplate.CreateContent();
    }
}

代码信用和来自David Britch's blog

的更多详细信息

然后在DataTemplate的{​​{1}}中,设置ExtendedFlexLayout,以使Flexlayout知道将您的项目分为3列

FlexLayout.Basis=33%

ExtendedFlexlayout中也不需要 <StackLayout Orientation="Horizontal" FlexLayout.Basis="33%"> <CheckBox HorizontalOptions="Start" Color="Black" CheckedChanged="CheckBox_CheckedChanged" IsChecked="{Binding IsSelected}"/> <Label Text="{Binding Name}" TextColor="Gray"></Label> </StackLayout>

,

ListView 目前不支持平铺视图。为什么不使用 CollectionView ?如果您不想使用它,则可以从nuget安装插件 FlowListView

<flv:FlowListView FlowColumnCount="2" FlowItemsSource="{Binding xxx}" >

    <flv:FlowListView.FlowColumnTemplate>
        <DataTemplate>

           //...

        </DataTemplate>
    </flv:FlowListView.FlowColumnTemplate>

</flv:FlowListView>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。