Extension Methods for WPF Infragistics Controls in C# Programming


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;

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

              public static void ClearAllSelected(this XamDataGrid xamDataGrid)
                   //Clearing all Selected rows.(if any)

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


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


Infragistics Help

Related Articles

About the Author

Srinath M S

I would love to leave a footprint in this flat world


  • 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: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • The explosion in mobile devices and applications has generated a great deal of interest in APIs. Today's businesses are under increased pressure to make it easy to build apps, supply tools to help developers work more quickly, and deploy operational analytics so they can track users, developers, application performance, and more. Apigee Edge provides comprehensive API delivery tools and both operational and business-level analytics in an integrated platform. It is available as on-premise software or through …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds