开发者

Filter data in ListView [mvvm]

开发者 https://www.devze.com 2023-01-30 16:11 出处:网络
I have created a listview (in xaml), where I have used GridViewColumn with DisplayMemberBinding to show properties of my ViewModel.

I have created a listview (in xaml), where I have used GridViewColumn with DisplayMemberBinding to show 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 fine. I can see the content of DataContent in my listview. Now I want to have a kind of listview filter. The user can enter in a textbox a string which is currently searched in every item in DataContent. The items in DataContent have a boolean property named "IsShown". This property is set to false, when the entered string is not found in the property "UserName" otherwise true.

What I need now is: Every row in the listview, which does not have a matching UserName must be hidden. I think I can use here somehow the property IsShown (in view model), which is available for each row. When the user removes or changes the s开发者_StackOverflow中文版tring from the textbox the content of DataContent is searched again and IsShown is modified if necessary. The listview must be updated again. Only the rows are shown, where "IsShown" is true.

How can I do that?


You can make use of CollectionView class

See Here

as well specific filter example

Filter Example

0

精彩评论

暂无评论...
验证码 换一张
取 消