How to Implement Filter in Xamgrid using WPF and MVVM

Shikha Gupta
 
SharePoint Developer
June 17, 2017
 
Rate this article
 
Views
4062

In the last article we saw how to add a header checkbox in xamgrid. In this article we will learn how to implement filter in xamgrid and how to access a view model property in Xaml.cs file. I will even show how to loop only through visible rows in Xamgrid.

1. Open MainWindow.Xaml and after the endng tag of xamgrid columns add the following code:

 <ig:XamGrid.FilteringSettings>
     <ig:FilteringSettings AllowFiltering="FilterMenu"/>
  </ig:XamGrid.FilteringSettings>
 

Something like below:clip_image002

2. Open MainWindow.Xaml.cs and add following refernces

 using System.Data;
 using Infragistics.Controls.Grids;
 using XamgridWPFProject.ViewModel;
 

3. Copy paste the following code.

 private DataTable _dtXMLData;
         public DataTable DTXMLData
         {
             get { return _dtXMLData; }
             set
             {
                 _dtXMLData = value;
             }
         }
         //Acessing DataEditorTable from ViewModel and binding it to DTXMLData 
         private void GroupCompsGrid_DataObjectRequested(object sender, DataObjectCreationEventArgs e)
         {
             DTXMLData = ((XamgridViewModel)this.DataContext).DataEditorDataTable;
             if (DTXMLData != null && e.ObjectType.Equals(typeof(DataRowView)))
             {
                 e.NewObject = new DataView(DTXMLData).AddNew();
             }
         }
 

Here we are using DataEditorDataTable which we defined in XamgridViewModel.cs and binding it to DTXMLData and in Data ObjectRequested method.

4. Add this tag DataObjectRequested=”GroupCompsGrid_DataObjectRequested” Xamgrid tag.

Something like below:clip_image004

5. Now run the project and you will get the filter options.

clip_image006

After applying filter on AddressLine1 column we get the following screen.clip_image008

Note: There is one problem that after applying filter if we select all and click on import button then it will add all the compid and not only the visible ones, i.e. it should just add the visible rows and not all like below.

clip_image010

In order to solve the following issue we have to loop through visible rows and not all rows.

6. Create a VisibleRows Property in XamgridViewModel.cs

 private RowCollection _vr;
         public RowCollection VisibleRows
         {
             get { return _vr; }
             set
             {
                 if(_vr != value)
                 {
                     _vr = value;
                     RaisePropertyChanged("VisibleRows");
                 }
             }            
         }
 

7. Add a class name GridRowCollectionBehavior.cs clip_image012

8. Add the following reference:

clip_image014

9. And copy and paste the following code:

 using System.Windows.Interactivity;
 using Infragistics.Controls.Grids;
 

10. Add the following code in this class

 class GridRowCollectionBehavior : Behavior<XamGrid>   {   protected override void OnAttached()   {   base.OnAttached();   AssociatedObject.Loaded += AssociatedObject_Loaded;   }     private void AssociatedObject_Loaded(object sender, System.Windows.RoutedEventArgs e)   {   XamgridViewModel vm = AssociatedObject.DataContext as XamgridViewModel;   vm.VisibleRows = AssociatedObject.Rows;   }   }   11. Now we have to add the following lines of code in UserControl tag of MainWindow.Xaml  xmlns:behaviors="http://schemas.microsoft.com/expression/2010/interactivity"   xmlns:local="clr-namespace:XamgridWPFProject"   Something like below:clip_image01612. Add the following code after filtering settings code in MainWindow.Xaml  <behaviors:Interaction.Behaviors>   <local:GridRowCollectionBehavior/>  </behaviors:Interaction.Behaviors>   13. We have to modify a bit of ImportSelected method initially we looping in all the rows now we need to loop through visiblerows .  public void ImportSelected()   {   Cmpidlist = new List<string>();   foreach (var r in VisibleRows)   {   Row row = (Row)r;   DataRowView rowView = row.Data as DataRowView;   if (rowView["Select"] != DBNull.Value)   {   var isSelected = (bool)rowView["Select"];   if (isSelected)   {   Cmpidlist.Add(rowView["Compid"].ToString());   }   }   }   Cmpidlist = Cmpidlist.Distinct().ToList();   }   14. Now run the project after filtering and if you checked the header checkbox and click on Import then only the visible rows will be added to comp id list. clip_image018In the same way if anywhere you want to loop through visible rows then you can loop in the similar fashion.Happy Coding

Category : MVVM, WPF

Author Info

Shikha Gupta
 
SharePoint Developer
 
Rate this article
 
Sikha has been working in IT Industry for over 5 years. She holds a B-tech degree. She is passionate about learning and sharing the tricks and tips in .Net , ...read more
 

Leave a comment