Hiding feature attributes

A common customization is to prevent one or more particular attributes from being displayed and/or edited. To illustrate this, the following sample code is a project extension that globally hides a particular attribute when viewing a feature's attributes page.

The first thing the project extension will do is subscribe to the ViewFeatureAttributesControl's static ControlCreatingFeatureAttributes event. Make sure you also unsubscribe from it when the extension is unloaded.

public class HideAttributeExtension : ProjectExtension
{
  // Define two attribute names 
  string _columnToBeRemoved = "Firstname";

  protected override void Initialize()
  {
  }

  protected override void OnOwnerInitialized()
  {
    // Make sure we are on the UI thread because getting the 
    // EditFeatureAttributesPage may cause it to be created, which can only
    // occur on the UI thread.
    if (!MobileApplication.Current.Dispatcher.CheckAccess())
    {
      MobileApplication.Current.Dispatcher.BeginInvoke((ThreadStart)delegate()
      {
        OnOwnerInitialized();
      });
      return;
    }

    ViewFeatureAttributesControl.ControlCreatingFeatureAttributes += new EventHandler<ViewFeatureAttributesControlEventArgs>(ViewFeatureAttributesControl_ControlCreatingFeatureAttributes);
  }

  protected override void Uninitialize()
  {
    ViewFeatureAttributesControl.ControlCreatingFeatureAttributes -= new EventHandler<ViewFeatureAttributesControlEventArgs>(ViewFeatureAttributesControl_ControlCreatingFeatureAttributes);
  }
}

Next, in the event handlers, you will find the attribute of interest in and remove it from the control's Attributes collection.

void ViewFeatureAttributesControl_ControlCreatingFeatureAttributes(object sender, ViewFeatureAttributesControlEventArgs e)
{
  // find and remove the Firstname attribute from the collection
  foreach (FeatureAttribute attribute in e.Control.Attributes)
  {
    DataColumnFeatureAttribute columnAttribute = attribute as DataColumnFeatureAttribute;
    if (columnAttribute == null)
      continue;

    if (columnAttribute.ColumnName.ToLower() == _columnToBeRemoved.ToLower())
    {
      e.Control.Attributes.Remove(attribute);
      break;
    }
  }
}
1/7/2015