简体   繁体   中英

Filter List view on item click - uwp

I have a list view which shows main subject and optional subject of students selected. Now I want to filter the listview when clicking on each row of it. The filtering should be based on main subject and optional subject. Means filtered rows of listview either contains any of main subject or optional subject.

 <ListView x:Name="ItemListView"  Width="Auto" Height="Auto" HorizontalAlignment="Stretch">
        <ListView.ItemTemplate>
            <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding StudentName}" TextAlignment="Left" FontSize="20"  Width="50"/>
                    </StackPanel>
                    <StackPanel>
                        <TextBlock Text="{Binding MainSub}" FontSize="20" TextAlignment="Center"   />
                    </StackPanel>
                    <StackPanel>
                        <TextBlock Text="{Binding OptionalSub}" FontSize="20" TextAlignment="Center" />
                    </StackPanel>
                    <StackPanel >
                        <TextBlock Text="{Binding RollNo}" FontSize="20" TextAlignment="Center" />
                    </StackPanel>    
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

  protected override async void OnNavigatedTo(NavigationEventArgs e)
    {
        ItemDetails messageData = new ItemDetails();
        ItemListView.ItemsSource = messageData.Collection;
        ItemListView.SelectedIndex = 0;
    }
   public class ItemDetails
    {
        public ItemDetails()
        {
            MatchList item;

            item = new MatchList();
            item.StudentName = "FF";
            item.MainSub= selectedSub[0].ToString();//English
            item.OptionalSub =selectedSub[1].ToString();//Sanskrit
            item.RollNo = 922;
            Collection.Add(item);

            item = new MatchList();
            item.StudentName = "DD";
            item.MainSub= selectedSub[0].ToString();//English
            item.OptionalSub =selectedSub[2].ToString();//Arabic
            item.RollNo = 82;
            Collection.Add(item);

             item = new MatchList();
            item.StudentName = "CC";
            item.MainSub= selectedSub[3].ToString();//Science
            item.OptionalSub =selectedSub[2].ToString();//Arabic
            item.RollNo = 12;
            Collection.Add(item);

             item = new MatchList();
            item.StudentName = "BB";
            item.MainSub= selectedSub[3].ToString();//Science
            item.OptionalSub =selectedSub[4].ToString();//Moral Science
            item.RollNo = 22;
            Collection.Add(item);

             item = new MatchList();
            item.StudentName = "AA";
            item.MainSub= selectedSub[0].ToString();//English
            item.OptionalSub =selectedSub[1].ToString();//Sanskrit
            item.RollNo = 322;
            Collection.Add(item);
        }
        List<MatchList> collection = new List<MatchList>();
        public List<MatchList> Collection
        {
            get
            {
                return this.collection;
            }
        }
    }

Well it seems several things are not yet complete in this sample code...

First up I suggest you use an ObservableCollection for the actual Collection property - this is needed so that your View will be notified if elements are added or removed from the collection.

Secondly you'll want to hook up a CollectionViewSource to your ListView ItemsSource and set it's Source to the ObservableCollection Collection property.

Last thing to do is then react to the ItemSelected of the ListView to filter out elements from the ObservableCollection . It should be reflected back to the ListView thanks to Data Binding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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