gpt4 book ai didi

c# - Xamarin 表格 : Binding the properties an ObservableCollection of custom class objects to a ListView in C# and XAML

转载 作者:太空宇宙 更新时间:2023-11-03 22:41:34 29 4
gpt4 key购买 nike

我有以下类(class):

public class NameObject
{
public string Name { get; set; }
public bool Suggested { get; set; }
public bool Popular { get; set; }
public string Gender { get; set; }
public short Personality { get; set; }
public short Decade { get; set; }
public string Origin { get; set; }
public string Diminutives { get; set; }
public string ShortMeaning { get; set; }
public string LongMeaning { get; set; }
}

我用该对象的大约 500 个条目填充 MainPage 类中的 ObservableCollection。由于需要在其中填充 ListView 的选项卡导航,我还有另一个类(class)。

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="engME.YourFullNamesListPage">

<ListView x:Name="FullNamesList" ItemsSource="">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding ???}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage>

我的目标是用每个条目填充此 ListView,但仅填充 NameList 的某些属性。 (姓名、性别、简称)

public partial class YourFullNamesListPage : ContentPage
{

public YourFullNamesListPage()
{
InitializeComponent();
FullNamesList.ItemsSource= MainPage.NameList;
}
}

在此先感谢您的帮助!我已经尝试解决这个问题大约 9 个小时,但我仍在学习,但一直没有成功。

最佳答案

A Cell将具有您的 NameObject 的一个实例的范围类。

你应该能够做到这一点:

<ListView x:Name="FullNamesList" ItemsSource="">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding Name}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

如果你想显示更多信息,你需要找到一些方法,在 TextCell 中你也可以使用 Details .但您也可以使用 ViewCell 构建自己的设计。 .

例如,像这样:

<ViewCell>
<StackLayout>
<Label Text="{Binding Name}"/>
<Label Text="{Binding Gender}"/>
</StackLayout>
</ViewCell>

从评论中收集,您已声明 NameList收藏于ItemsSource作为成员,而不是属性(property)。为了能够绑定(bind)到一个字段,它必须声明为公共(public)属性。

更改 public static ObservableCollection<NameObject> NameList = new ObservableCollection<NameObject>();

进入

public static ObservableCollection<NameObject> NameList { get; set; } = new ObservableCollection<NameObject>();

关于c# - Xamarin 表格 : Binding the properties an ObservableCollection of custom class objects to a ListView in C# and XAML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51981776/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com