ListView项不适合网格单元格

ListView项不适合网格单元格,listview,xamarin.forms,grid,Listview,Xamarin.forms,Grid,请帮帮我。在Xamarin.Forms中,我有一个包含卡片的主列表视图(带有网格的框架)。每个网格都有标签和电话列表。带有电话的ListVew占据了三行三列。我需要网格内电话列表视图的所有元素始终可见(通常它们的数量最多为三个,但可能更多),并且网格单元的高度将在必要时增加,以容纳电话列表的所有元素。这可能吗?现在,网格单元格在标签中增加文本高度,但在PhoneListView高度中不增加 [MyListView MainView = new MyListView {

请帮帮我。在Xamarin.Forms中,我有一个包含卡片的主列表视图(带有网格的框架)。每个网格都有标签和电话列表。带有电话的ListVew占据了三行三列。我需要网格内电话列表视图的所有元素始终可见(通常它们的数量最多为三个,但可能更多),并且网格单元的高度将在必要时增加,以容纳电话列表的所有元素。这可能吗?现在,网格单元格在标签中增加文本高度,但在PhoneListView高度中不增加

[MyListView MainView = new MyListView
            {
                SeparatorVisibility = SeparatorVisibility.None,
                HasUnevenRows = true,

                ItemTemplate = new DataTemplate(() =>
                {
                    Grid MyGrid = new Grid
                    {
                        RowDefinitions =
                {
                new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) },
                new RowDefinition { Height = new GridLength(0.02, GridUnitType.Absolute) },
                new RowDefinition { Height = new GridLength(1, GridUnitType.Star) },
                new RowDefinition { Height = new GridLength(0.02, GridUnitType.Absolute) },
                new RowDefinition { Height = new GridLength(1, GridUnitType.Star) },
                new RowDefinition { Height = new GridLength(0.02, GridUnitType.Absolute) },
                new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) }
                },
                        ColumnDefinitions =
                {
                new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) },
                new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) },
                new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) },
                new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) },
                new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) },
                new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) },
                new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }
                }
                    };

...

ListView phonesList = new ListView
                    {
                        VerticalScrollBarVisibility = ScrollBarVisibility.Never,
                        BackgroundColor = Color.Transparent,
                        SeparatorVisibility = SeparatorVisibility.None,
                        HasUnevenRows = true,
                        VerticalOptions = LayoutOptions.FillAndExpand,
                        ItemTemplate = new DataTemplate(() =>
                        {
                            Image phoneButton = new Image { Source = "phone_green.png", HeightRequest = 40, VerticalOptions = LayoutOptions.Center };
                            Label phoneTextLabel = new Label { HeightRequest = 40, VerticalOptions = LayoutOptions.Center, VerticalTextAlignment = TextAlignment.Center };
                            phoneTextLabel.SetBinding(Label.TextProperty, "phone");
                            return new ViewCell { View = new StackLayout {
                                Orientation = StackOrientation.Horizontal,
                                Children = { phoneButton, phoneTextLabel } } };
                        })
                    };
phonesList.SetBinding(ListView.ItemsSourceProperty, "phones", BindingMode.Default, new PhoneSepConverter());
...
                    DetailGrid.Children.Add(phonesList, 4, 2); 
...
                    Grid.SetColumnSpan(phonesList, 3);
                    Grid.SetRowSpan(phonesList, 3);
...
Frame ListItem = new Frame { Content = MyGrid };
                    return new ViewCell
                    {
                        View = ListItem
                    };
})
            };][1]

使用带有BindableLayout的简单StackLayout,因为您不需要可滚动的视图

由于ListView中没有可用的
ItemSizingStrategy
,因此所有项目都呈现为与第一个项目相同的大小。这不适合你的事业

mainListView
使用
CollectionView
而不是
ListView
,并将
itemsizengstrategy
设置为
measurealitems

CollectionView mainListView=newcollectionview();
mainListView.ItemsSource=新列表()
{
新项(){Name=“First”,Phones=new List(){“Pnone 1”,},
新项目(){Name=“Second”,Phones=新列表(){“Pnone 1”、“Phone 2”、“Phone 4”},
新项(){Name=“Third”,Phones=新列表(){“Pnone 1”,“Phone 4”},
新项目(){Name=“Fourth”,Phones=新列表(){“Pnone 1”、“Phone 2”、“Phone 3”、“Phone 4”},
新项目(){Name=“Fifth”,Phones=新列表(){“Pnone 1”,“Phone 2”},
new Item(){Name=“Sixth”,Phones=new List(){“Pnone 1”,“Phone 2”,“Phone 3”},
新项(){Name=“Seventh”,Phones=new List(){“Phone 4”},
新项(){Name=“Eight”,Phones=新列表(){“Pnone 1”、“Phone 2”、“Phone 3”、“Phone 4”},
新项(){Name=“nixth”,Phones=新列表(){“Pnone 1”、“Phone 2”、“Phone 4”},
新项目(){Name=“Tenth”,Phones=新列表(){“Pnone 1”,“Phone 4”},
新项目(){Name=“11th”,Phones=新列表(){“Pnone 1”、“Phone 2”、“Phone 3”、“Phone 4”},
新项(){Name=“tweeth”,Phones=新列表(){“Pnone 1”、“Phone 2”、“Phone 3”},
新项目(){Name=“thirth”,Phones=新列表(){“Pnone 1”、“Phone 2”、“Phone 3”、“Phone 4”},
新项目(){Name=“十四”,电话=新列表(){“Pnone 1”,“电话2”},
};
mainListView.ItemSizingStrategy=ItemSizingStrategy.MeasureAllItems;
mainListView.ItemTemplate=新数据模板(()=>
{
框架=新框架()
{
转弯半径=0,
背景颜色=颜色。灰色
};
Grid mainItemGrid=新网格()
{
列定义=
{
new ColumnDefinition(){Width=new GridLength(1,GridUnitType.Star)}
}
};
StackLayout StackLayout=新的StackLayout()
{
方向=堆叠方向。垂直
};
stackLayout.SetBinding(bindablellayout.ItemsSourceProperty,“Phones”);
BindableLayout.SetItemTemplate(stackLayout,新数据模板(()=>{
标签=新标签();
label.SetBinding(label.TextProperty,“.”);
退货标签;
}));
mainItemGrid.Children.Add(
新标签(){Text=“Some Text”}
, 0, 0);
mainItemGrid.Children.Add(
堆栈布局,1,0);
帧填充=1;
frame.Content=mainItemGrid;
mainItemGrid.BackgroundColor=Color.White;
返回框;
});
用户界面结果


请检查这是否有助于您……

这是屏幕截图的链接,您的意思是让所有ListItem在初始时都可见吗?不需要滚动?是的。不需要滚动。那么为什么不使用StackLayout with而不是ListView?不知道BindableLayout,我会试试的,谢谢!替换mainListView并没有解决我的问题,但我用stackLayout phonesList+BindableLayout替换了ListView phonesList,并将RowDefinition高度从星形设置为自动,这就解决了我的问题!再次感谢!