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

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

  • Download the Information Governance Survey Benchmark Report to gain insights that can help you further establish business value in your Records and Information Management (RIM) program and across your entire organization. Discover how your peers in the industry are dealing with this evolving information lifecycle management environment and uncover key insights such as: 87% of organizations surveyed have a RIM program in place 8% measure compliance 64% cannot get employees to "let go" of information for …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds