Xamarin 形成动态列表视图绑定
Xamarin Forms Dynamic Listview Binding
我正在尝试创建此 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();
}
因为我只需要在列表视图中显示 1 列,所以我添加了一个新列表来处理 ObjectCollection 中的结果。这样我就不需要更改绑定。
private async Task initLvw()
{
ctype = ClassId;
switch (ClassId)
{
case "brand":
collection = await dbItem.ReadData(e=>e.brand.ToLower().Contains(searchBar.Text.ToLower()));
list = new List<string>(collection.Select(i=>i.brand).Distinct());
break;
case "part":
collection = await dbItem.ReadData(e => e.partno.ToLower().Contains(searchBar.Text.ToLower()));
list = new List<string>(collection.Select(i => i.partno).Distinct());
break;
case "plgrp":
collection = await dbItem.ReadData(e => e.plgrp.ToLower().Contains(searchBar.Text.ToLower()));
list = new List<string>(collection.Select(i => i.plgrp).Distinct());
break;
case "itemname":
collection = await dbItem.ReadData(e => e.itemname.ToLower().Contains(searchBar.Text.ToLower()));
list = new List<string>(collection.Select(i => i.itemname).Distinct());
break;
}
lvw.ItemsSource = list;
lvw.EndRefresh();
}
我正在尝试创建此 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();
}
因为我只需要在列表视图中显示 1 列,所以我添加了一个新列表来处理 ObjectCollection 中的结果。这样我就不需要更改绑定。
private async Task initLvw()
{
ctype = ClassId;
switch (ClassId)
{
case "brand":
collection = await dbItem.ReadData(e=>e.brand.ToLower().Contains(searchBar.Text.ToLower()));
list = new List<string>(collection.Select(i=>i.brand).Distinct());
break;
case "part":
collection = await dbItem.ReadData(e => e.partno.ToLower().Contains(searchBar.Text.ToLower()));
list = new List<string>(collection.Select(i => i.partno).Distinct());
break;
case "plgrp":
collection = await dbItem.ReadData(e => e.plgrp.ToLower().Contains(searchBar.Text.ToLower()));
list = new List<string>(collection.Select(i => i.plgrp).Distinct());
break;
case "itemname":
collection = await dbItem.ReadData(e => e.itemname.ToLower().Contains(searchBar.Text.ToLower()));
list = new List<string>(collection.Select(i => i.itemname).Distinct());
break;
}
lvw.ItemsSource = list;
lvw.EndRefresh();
}