【问题标题】:Xamarin Forms Dynamic Listview BindingXamarin 表单动态列表视图绑定
【发布时间】: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();

    }

【问题讨论】:

标签: xamarin.forms model-binding


【解决方案1】:

由于我只需要在列表视图中显示 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();

    }

【讨论】:

  • 很高兴听到您自己解决了您的问题,请记得将您的回复标记为答案来关闭您的帖子,这有利于其他人找到此答案,谢谢。
猜你喜欢
  • 2019-08-17
  • 2015-04-22
  • 2020-01-12
  • 2016-12-06
  • 1970-01-01
  • 2017-09-06
  • 1970-01-01
  • 2023-03-12
  • 2017-11-29
相关资源
最近更新 更多