嵌套 ListView 在 xamarin 表单中不起作用 [英] Nested ListView is not working in xamarin forms
问题描述
嵌套列表视图不起作用,我有一个列表,其中包含另一个列表.为了在视图中显示它,我使用了嵌套列表视图;但是代码不起作用,我无法确定哪里出错了......下面是我的代码
Nested list view is not working, I have a list which contains another list in it. To show it in View I am using Nested listview; But the code is not working,and i am not able to identify on where it went wrong... Below is my code
主页
<ContentPage.Content>
<StackLayout>
<ListView x:Name="outerListview" ItemsSource="{Binding lst}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell x:Name="outerListviewCell">
<ViewCell.View>
<ContentView>
<Label Text="{Binding ItemName}"/>
<StackLayout>
<ListView ItemsSource="{Binding ItemList}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell x:Name="InnerListviewCell">
<Grid>
<Label Text="{Binding stockQty}"/>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentView>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
视图模型
public MainPageViewModel()
{
lst = new ObservableCollection<A>()
{
new A()
{
ItemName="Item1", ItemList=new ObservableCollection<ItemDetails>()
{
new ItemDetails() { stockQty="2"},
new ItemDetails(){ stockQty="3"}
}
},
new A()
{
ItemName="Item2", ItemList=new ObservableCollection<ItemDetails>()
{
new ItemDetails() { stockQty="3"},
new ItemDetails(){ stockQty="4"}
}
}
};
}
模型(A 类和类项目详细信息)
class A:INotifyPropertyChanged
{
public A()
{
ItemName = string.Empty;
ItemList = new ObservableCollection<ItemDetails>();
}
private string _ItemName;
public string ItemName
{
get { return _ItemName; }
set { _ItemName = value; OnPropertyChanged(); }
}
private ObservableCollection<ItemDetails> _itemlist;
public ObservableCollection<ItemDetails> ItemList
{
get { return _itemlist; }
set { _itemlist = value; OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
if (PropertyChanged != null)
PropertyChanged(this,
new PropertyChangedEventArgs(propertyName));
}
}
class ItemDetails:INotifyPropertyChanged
{
private string _stockQty;
public string stockQty
{
get { return _stockQty; }
set { _stockQty = value; OnPropertyChanged(); }
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
if (PropertyChanged != null)
PropertyChanged(this,
new PropertyChangedEventArgs(propertyName));
}
}
当我运行上面的代码时,我在屏幕上的输出低于
When I Run the above code I am getting below output in screen
2
3
实际是什么
Item1
2
3
Item2
3
4
上面的代码有什么问题?有人可以帮我吗?
What is wrong in above code? could anyone can help me?
推荐答案
将 Listview
嵌套在另一个 Listview
中不是一个好主意,它在 Xamarin 上不受支持.形式.
Nesting Listview
inside another Listview
is not a good idea and it is not a supported on Xamarin.Forms.
ListView
非常敏感",它很容易导致滚动问题,当然还有应用性能不佳的问题.
ListView
is very "sensitive" and it can easialy cause problems with scrolling and of course there are problems with poor performance of your app.
所以我强烈建议您重新考虑您的布局并查看使用 ListView
分组的更多信息这里,也许你可以通过分组来实现你想要的.
So I strongly recommend you to rethink about your layout and take a look at Grouping with ListView
more about it here, maybe you can achieve what you want with Grouping.
这篇关于嵌套 ListView 在 xamarin 表单中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!