【发布时间】:2020-05-22 18:06:31
【问题描述】:
我正在尝试创建这个 ContentView,它将根据 ClassID 从数据库中获取不同的列。我在显示正确数据时遇到问题。我不确定我的方法是否正确?我可以使用我的变量 cType 来更改 ViewCell 内标签的绑定
我如何调用 ContentView
<local:AutoCompleteEntry
x:Name="vBrand"
ClassId="brand"
/>
<local:AutoCompleteEntry
x:Name="vPart"
ClassId="part"
/>
XAML
<StackLayout Orientation="Vertical" BackgroundColor="AliceBlue">
<Entry TextChanged="searchBar_TextChanged"
BackgroundColor="#f9f9f9"
TextColor="#FF464859"
Unfocused="searchBar_Unfocused"
FontSize="16" PlaceholderColor="#646b7a"
x:Name="searchBar"
Placeholder="Type here..."/>
<ListView x:Name="lvw" IsVisible="False"
CachingStrategy="RecycleElement"
BackgroundColor="White"
ItemTapped="lvw_ItemTapped">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Frame>
<StackLayout BackgroundColor="White">
<Label Text="{Binding ctype}" FontSize="16" TextColor="Black"/>
</StackLayout>
</Frame>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
代码
ObservableCollection<Item> collection;
SqliteDB<Item> dbItem = new SqliteDB<Item>(unique.dbPath);
String ctype="";
private async Task initLvw()
{
ctype = ClassId;
switch (ClassId)
{
case "brand":
collection = await dbItem.ReadData(e=>e.brand.Contains(searchBar.Text));
lvw.ItemsSource = collection.Select(i => i.brand);
break;
case "part":
collection = await dbItem.ReadData(e => e.partno.Contains(searchBar.Text));
lvw.ItemsSource = collection.Select(i => i.partno);
break;
case "plgrp":
collection = await dbItem.ReadData(e => e.plgrp.Contains(searchBar.Text));
lvw.ItemsSource = collection.Select(i => i.plgrp);
break;
case "itemname":
collection = await dbItem.ReadData(e => e.itemname.Contains(searchBar.Text));
lvw.ItemsSource = collection.Select(i => i.itemname);
break;
}
lvw.EndRefresh();
}
【问题讨论】:
-
我可以使用我的变量 cType 来更改 ViewCel 内标签的绑定您可以考虑使用 DataTemplateSelector 来显示不同的列以进行 ListView 绑定,这是关于数据模板选择器:docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/…
标签: xamarin.forms model-binding