简体   繁体   English

Xamarin表示C#ListView搜索

[英]Xamarin Forms C# ListView Search

Xamarin Forms C# ListView Search Xamarin表示C#ListView搜索
Hello everyone, i want to make a search from ListView when i make filter by name i want suggestions appear in listview I tried to do this but not working for me !! 大家好,我想从ListView进行搜索,当我按名称进行过滤我希望建议出现在listview中我试图这样做但不适合我! i don't understand what is the problem.. 我不明白是什么问题..

Screenshot for the problem: https://i.imgur.com/vhpt8Cp.jpg 问题的屏幕截图: https//i.imgur.com/vhpt8Cp.jpg

Xamarin Forms C# ListView Search   

Hello everyone, i want to make a search from ListView when i make filter by name i want suggestions appear in listview I tried to do this but not working for me !! 大家好,我想从ListView进行搜索,当我按名称进行过滤我希望建议出现在listview中我试图这样做但不适合我! i don't understand what is the problem.. 我不明白是什么问题..

    Model> echeance.cs:
    ------------

    namespace Appz.Model
    {
        public class Echeance
        {

            [PrimaryKey, AutoIncrement]
            public int ChkId { get; set; }

            public string ChkChoise { get; set; }

            [MaxLength(50)]
            public string ChkNumber { get; set; }

            public string ChkAmount { get; set; }

            [MaxLength(50)]
            public string BeneficiaryName { get; set; }

            public string AgencyType { get; set; }

            public DateTime ChkDueDate { get; set; }

            public DateTime ChkReleaseDate { get; set; }

            public string ChkImage { get; set; }


        }
    }





    EcheanceView.Xaml:
    -----------------

    <?xml version="1.0" encoding="UTF-8"?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                  xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core" 
        ios:Page.UseSafeArea="true"
                 x:Class="Appz.View.EcheanceView">
        <ContentPage.Content>
            <StackLayout Padding="10,0,10,0">
                <SearchBar TextChanged="Handle_TextChanged"></SearchBar>
                <ListView  ItemsSource="{Binding EcheancesList}" IsPullToRefreshEnabled="True"  x:Name="ListEcheances" HasUnevenRows="true" SeparatorVisibility="Default" ItemTapped="OnItemSelected">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <StackLayout Orientation="Horizontal">
                                    <Image Source="{Binding ChkImage}" HeightRequest="50" WidthRequest="50"  />
                                    <StackLayout HorizontalOptions="StartAndExpand">
                                        <Label Text="{Binding BeneficiaryName}"
                                           FontAttributes="Bold" />
                                        <Label Text="{Binding AgencyType}"
                                           TextColor="Gray" />
                                    </StackLayout>
                                    <Button Text="Follow Me"
                                        BorderColor="Silver"
                                       FontSize="Small"
                                       TextColor="White"
                                       BackgroundColor="#3399ff"
                                       VerticalOptions="Center"
                                      />
                                </StackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

            </StackLayout>

        </ContentPage.Content>
    </ContentPage>





    EcheanceView.cs:
    ---------------

    void Handle_TextChanged(object sender, Xamarin.Forms.TextChangedEventArgs e)
            {


                using (SQLiteConnection conn = new SQLiteConnection(App.DatabaseLocation))
                {               
                    var echeances = conn.Table<Echeance>().ToList();
                    //ListEcheances.ItemsSource = echeances;

                    if (string.IsNullOrWhiteSpace(e.NewTextValue))
                        ListEcheances.ItemsSource = echeances;
                    else
                        ListEcheances.ItemsSource = echeances.Where(i => i.BeneficiaryName.Contains(e.NewTextValue));

                    ListEcheances.EndRefresh();

                }                    

            }

someone help me please Thanks in advance !!! 有人帮助我,请提前致谢!!!

The approach I would go with for real-time filtering, is having two data sets, a backing list and a filtered collection that dynamically changes (this is the one you should bind your ListView's ItemSource to). 我将用于实时过滤的方法是拥有两个数据集,一个支持列表和一个动态更改的过滤集合(这是你应该将ListView的ItemSource绑定到的集合)。 Then just bind your Entry's Text property to your corresponding property (SearchTerm in this example). 然后将Entry的Text属性绑定到相应的属性(本例中为SearchTerm)。 In the setter you could execute your filter/search command. 在setter中,您可以执行filter / search命令。

  private ObservableCollection<Test> filteredItems;
    public ObservableCollection<Test> FilteredItems
    {
        get => filteredItems;
        set
        {
            // Notify property changed
        }
    }

    private List<Test> allItems;
    public List<Test> AllItems
    {
        get => allItems;
        set
        {
            // Notify property changed
        }
    }

    private string searchTerm;
    public string SearchTerm
    {
        get => searchTerm;
        set
        {
            // Notify property changed
            SearchCommand.Execute(searchTerm);
        }
    }

    public Command SearchCommand
    {
        get
        {
            return new Command<string>((searchString) =>
            {
                FilteredItems = new ObservableCollection<Test>(AllItems.Where(o => o.itemText.ToLower().Contains(searchString.ToLower())));

            });
        }
    }

在您的情况下,似乎某些项目的BeneficiaryName为null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM