0
votes

I'm trying to create this ContentView where it will get different columns from the database based on the ClassID. I'm having a problem when it comes to displaying the correct data. I'm not sure if my approach is the right way? Can I use my variable cType to change the binding of the label inside the ViewCell

How I called the 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>

CODE

    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
Can I use my variable cType to change the binding of the label inside the ViewCel you may consider to use DataTemplateSelector to display different column for ListView binding, this is the article about DataTemplateSelector:docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/…Cherry Bu - MSFT

1 Answers

0
votes

Since I just need to display 1 column in the listview, I added a new list that will handle the results in the ObjectCollection. that way I don't need to change the binding.

    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();

    }