We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

FILTERING SFLISTVIEW USING MVVM PATTERN

Hi,
Do you have a sample project showing how to filter an SFListView using MVVM pattern. The page will have a SearchBar which will determine the filter criteria. I saw an example in your documentation but it wasn't using MVVM.
Thanks.

5 Replies

DB Dinesh Babu Yadav Syncfusion Team May 20, 2019 04:24 AM UTC

Hi Bamame, 
 
Thanks for contacting Syncfusion support.  
  
We have prepared sample using SfListView with filtering in MVVM pattern and attached the sample in following link. Please find the sample for your reference.  
  
  
To know more about filtering, please refer our UG documentation.  
  
 
Please let us know if you require further assistance. 
 
Regards, 
Dinesh Babu Yadav 



BA Bamame May 21, 2019 03:56 AM UTC

Thanks Dinesh for the sample project. I saw the UG link but that wasn't what I was looking for. I will go through the project and let you know if I have any questions.

Thanks again!


DB Dinesh Babu Yadav Syncfusion Team May 21, 2019 04:12 AM UTC

Hi Bamame, 
 
Thanks for the update. We will wait to hear from you. 
 
Regards, 
Dinesh Babu Yadav 



PP Paul Parkins March 29, 2021 09:59 AM UTC

Whilst this might work it's not a great example of MVVM code as the OnTextChanged implementation within the view model, relies on the specific view you have provided, which is not good MVVM practise. It expects the sender to be a grid, and to find the first child to be a searchbar., and the second child to be the list.

In my case I programmatically know what I want the filter to be, but need some way of setting the Datasource.Filter and triggering RefreshFilter ideally via binding to the view model, rather than the view model (or code behind) code using direct access of the view's listview.

It seems that although Datasource can be referenced in the xaml view, Datasource.Filter cannot.



LN Lakshmi Natarajan Syncfusion Team March 30, 2021 04:59 AM UTC

Hi Paul, 
 
Thank you for using Syncfusion products. 
 
We have checked the reported query “Filter items using MVVM” from our side. We would like to inform you that you can achieve your requirement using MVVM, by binding the SfListView.DataSource property. Please refer to the following code snippets for more reference, 
 
ViewModel: Create DataSource property in the ViewModel and add filter predicate in the FilterCommand execute method. 
public class SortingFilteringViewModel : INotifyPropertyChanged 
{ 
    private string searchText = null; 
 
    public ObservableCollection<TaskInfo> Items { get; set; } 
    public Command<object> FilterCommand { get; set; } 
    public DataSource FilterDataSource { get; set; } 
         
    public SortingFilteringViewModel() 
    { 
        FilterCommand = new Command<object>(OnTextChanged); 
        FilterDataSource = new DataSource(); 
        FilterDataSource.Source = Items; 
 
        AddItemDetails(); 
    } 
 
    private void OnTextChanged(object obj) 
    { 
        searchText = obj as string; 
 
        if (FilterDataSource != null) 
        { 
            FilterDataSource.Filter = FilterContacts; 
            FilterDataSource.RefreshFilter(); 
        } 
    } 
 
    private bool FilterContacts(object obj) 
    { 
        var taskInfo = obj as TaskInfo; 
        return (taskInfo.Title.ToLower().Contains(searchText.ToLower()) 
            || taskInfo.Description.ToLower().Contains(searchText.ToLower())); 
    } 
} 
 
 
XAML: Bind the ViewModel DataSource property to the SfListView.DataSource property. 
<sync:SfListView x:Name="listView" 
                ItemsSource="{Binding Items}"  
                DataSource="{Binding FilterDataSource}" 
                Grid.Row="1" 
                SelectionMode="None" 
                ItemSpacing="0,5,0,5" 
                BackgroundColor="#F0F0F0" 
                ItemSize="120"> 
 
 
We have prepared a filter sample using MVVM and attached in the following link, 
 
Also, you can achieve the requirement using Behavior. Please refer to our documentation regarding the same, 
 
Please let us know if you need further assistance. 
 
Lakshmi Natarajan 
  
 


Loader.
Live Chat Icon For mobile
Up arrow icon