Filter data in ListView [mvvm]

I created a list (in xaml) where I used a GridViewColumn with DisplayMemberBinding to show the properties of my ViewModel.

     <ListView x:Name="MyListView" ItemsSource="{Binding DataContent}">
        <ListView.View>
           <GridView>
              <GridViewColumn DisplayMemberBinding="{Binding UserName}"/>
              <GridViewColumn DisplayMemberBinding="{Binding LastName}"/>
           </GridView>
        </ListView.View>
     </ListView>

Everything works perfectly. I see the contents of the DataContent in my list. Now I want to have a list filter view. The user can enter in the text box the line that is currently running in each DataContent element. Elements in a DataContent have a boolean property called "IsShown". This property is false when the entered string is not found in the "UserName" property otherwise.

Now I need: Each line in the list that does not have a corresponding UserName must be hidden. I think I can somehow use the IsShown property (in the view model), which is available for each row. When a user deletes or modifies a string from a text field, the contents of the DataContent are executed again, and IsShown is changed if necessary. The list should be updated again. Only lines where "IsShown" is true are displayed.

How can i do this?

+3
source share
1 answer

You can use the CollectionView class

See here

as well as a specific filter example

Filter example

+2
source

Source: https://habr.com/ru/post/1780607/


All Articles