Extension Methods for WPF Infragistics Controls in C# Programming

Introduction

As stated in my previous article titled "Enhancing Windows Presentation Foundation (WPF) XamDataGrid from Infragistics", Infragistics, one of the leading third-party control providers for the .NET framework has sufficient controls that adhere to my common tasks.

Extension methods make C# programming code readable and developer-friendly. UI components generally tend to expose events and common routines which are at times repeatitive and time consuming. This article touches base on one such UI component from Infragistics and looks at how extension methods can help us out.

As MSDN points out : "Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. ".

It's also clearly defined by Scott Hanselman in one of his blog post on Extension methods: "Extension methods are a new feature in .NET framework 3.5 (C#3/VB9) that let you appear to "spot weld" new methods on to existing classes. If you think that the "string" object needs a new method, you can just add it and call it on instance variables."

Extension methods also let you have control over error handling. It's not easy to wrap every piece of code with condition checks such as if some object > 0...etc. These can be very well written at a single place. Statements like this:

  xamDataGrid.FieldLayouts[0].Fields[fieldName].Visibility = Visibility.Visible;

Can throw an error if there are no fieldlayouts defined at all. The extension methods also reduce development time once developed and make the code easy to read.

Let's first consider the need for each method and then see its underlying code snippet in extension methods format:

  1. If you want to make a Record Active and Selected for the user, you would use the "IsActive" and the "IsSelected" properties. You would toggle the values for each of the above said properties. This can be easily accomodated in the extension method shown below:

                 public static void ActivateRecord(this Record record, bool  shouldActivate)
                 {
                     record.IsActive 	= shouldActivate;
                     record.IsSelected 	= shouldActivate;
                 }
     

  2. If you want to want to hide certain columns in a XamDataGrid, you would use the Visibility property of the field. This needs to be done after two conditions are checked (i.e., the Field layout existence and the field existence). This can be easily accommodated in the extension method shown below:

                 public static bool  ContainsFieldLayouts(this XamDataGrid xamDataGrid)
                 {
                      return xamDataGrid.FieldLayouts.Count  > 0);
                  }
       
               We will use the ContainsFieldLayouts methods in our actual Extension method for                    	hiding a column in the XamDataGrid. 
       
                public static void HideField(this XamDataGrid xamDataGrid, string fieldName)
                {
                   if (xamDataGrid.ContainsFieldLayouts() && 
       	xamDataGrid.FieldLayouts[0].Fields.IndexOf(fieldName) !=   -1)
                       xamDataGrid.FieldLayouts[0].Fields[fieldName].Visibility = Visibility.Collapsed;
                 }
     

  3. Similarly, if you want to show a certain field in a XamDataGrid, you would use the visibility property of the field but this too needs to be done after two conditions are checked (i.e. the field layout existence and the field existence). Look below:

       public static void ShowField(this XamDataGrid xamDataGrid, string fieldName)
                  {
                     if (xamDataGrid.ContainsFieldLayouts() && 
                          xamDataGrid.FieldLayouts[0].Fields.IndexOf(fieldName) != -1)
                         xamDataGrid.FieldLayouts[0].Fields[fieldName].Visibility = Visibility.Visible;
                  }
     

  4. If you want to set a label for a field, you can do it in a easier manner with the method below:

       public static void SetLabel(this XamDataGrid xamDataGrid, string fieldName, string label)
                 {
                       if (xamDataGrid.ContainsFieldLayouts() &&  xamDataGrid.FieldLayouts[0].Fields.IndexOf(fieldName) 	!= -1)
                         xamDataGrid.FieldLayouts[0].Fields[fieldName].Label = label;
                 }
     

  5. Setting a tooltip on scroll for a field is achieved using the IsScrollTipField property. Look below at the extension method:

               public static void SetScrollTipField(this XamDataGrid xamDataGrid, string fieldName)
               {
                   if (xamDataGrid.ContainsFieldLayouts() &&  xamDataGrid.FieldLayouts[0].Fields.IndexOf(fieldName) 	!= -1)
                       xamDataGrid.FieldLayouts[0].Fields[fieldName].IsScrollTipField = true;
               }
     

  6. An extension method to deterimine if a record is a groupby record. Compare the type of the record:

              public static bool IsGroupByRecord(this Record record)
              {
                   return (record.GetType() == typeof(GroupByRecord));
              }
     

  7. To set a read-only use the Allow Edit property of the Field Settings.

               public static void SetFieldReadonly(this XamDataGrid xamDataGrid, string fieldName)
               {
       	if (xamDataGrid.ContainsFieldLayouts() &&  xamDataGrid.FieldLayouts[0].Fields.IndexOf(fieldName) 	!= -1)
                       xamDataGrid.FieldLayouts[0].Fields[fieldName].Settings.AllowEdit = false;
               }
     

  8. To Save an active record you can use the ExecuteCommand feature of the XamDataGrid. This in turn will trigger the DataPresenterCommands. In this case, the CommitChangesToActiveRecord is called.

               public static void SaveActiveRecord(this XamDataGrid xamDataGrid, DataRecord dataRecord)
               {
                    dataRecord.IsActive = true;
                    xamDataGrid.ExecuteCommand(DataPresenterCommands.CommitChangesToActiveRecord);
               }
     

  9. To clear all selected records use the ClearAllSelected DataPresenterCommand.

              public static void ClearAllSelected(this XamDataGrid xamDataGrid)
               {
                   //Clearing all Selected rows.(if any)
                   xamDataGrid.ExecuteCommand(DataPresenterCommands.ClearAllSelected);
          }
     

Sample Code

  private void MyXamDataGrid_FieldLayoutInitialized (object sender, Infragistics.Windows.DataPresenter.Events.FieldLayoutInitializedEventArgs e)
  {
   
        //Setting field read-only.
        MyXamDataGrid.SetFieldReadonly (MyConstants.ID);
   
       //Hide a field.
       MyXamDataGrid.HideField (MyConstants.PRIMARYPHONE);
       MyXamDataGrid.HideField (MyConstants.SECONDARYPHONE);
                                      
       //Show a field.
        MyXamDataGrid.ShowField (MyConstants.FIRSTNAME);
        MyXamDataGrid.ShowField (MyConstants.MIDDLENAME);
        MyXamDataGrid.ShowField (MyConstants.LASTNAME);
   
      //Set the scroll tip field.
        MyXamDataGrid.SetScrollTipField (MyConstants.SURNAME);
   
        //To save active Record
        MyXamDataGrid.SaveActiveRecord (myRecord);
  }

The Infragistics Library Used

Infragistics WPF 2008 Volume 2.

  • Infragistics3.Wpf.v8.2
  • Infragistics3.Wpf.Ribbon.v8.2
  • Infragistics3.Wpf.Editors.v8.2
  • Infragistics3.Wpf.Reporting.v8.2
  • Infragistics3.Wpf.DataPresenter.v8.2.dll

Conclusion

I hope you found this short tutorial helpful. Please leave your comments below.

References

MSDN
Infragistics Help

Related Articles



About the Author

Srinath M S

I would love to leave a footprint in this flat world

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

RSS Feeds