Hiding feature attributes

One customization use case is to prevent a particular attribute from being displayed and/or edited. To illustrate this, you will write a project extension that does two things: (1) globally hide a particular attribute when viewing a feature's attributes and (2) prevent a particular attribute from being edited on the View Updates page.

The first thing the project extension will do is subscribe to the ViewFeatureAttributesControl's static ControlCreatingFeatureAttributes event and the CreatingFeatureAttributes event from EditFeatureAttributesPage within the synchronization task/workflow.

public class  HideAttributeExtension : ProjectExtension
{
  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;
    }
 
    // Listen to the static event for the ViewFeatureAttributesControl so we
    // customize the view attributes control globally.
    ViewFeatureAttributesControl.ControlCreatingFeatureAttributes += new EventHandler<ViewFeatureAttributesControlEventArgs>(ViewFeatureAttributesControl_ControlCreatingFeatureAttributes);
 
    // Listen to the event from the edit feature attributes control from the
    // View Updates page.
    SynchronizeTask syncTask = MobileApplication.Current.FindTask(typeof(SynchronizeTask)) as SynchronizeTask;
    if (syncTask != null)
    {
      _editAttributesPage = syncTask.EditFeatureAttributesPage;
      _editAttributesPage.CreatingFeatureAttributes += new EventHandler(_editAttributesPage_CreatingFeatureAttributes);
    }
  }
 
  protected override void Uninitialize()
  {
    ViewFeatureAttributesControl.ControlCreatingFeatureAttributes -= new EventHandler<ViewFeatureAttributesControlEventArgs>(ViewFeatureAttributesControl_ControlCreatingFeatureAttributes);
    if (_editAttributesPage != null)
    {
      _editAttributesPage.CreatingFeatureAttributes -= new EventHandler(_editAttributesPage_CreatingFeatureAttributes);
      _editAttributesPage = null;
    }
  }

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 AS_BUILT attribute from the collection
  foreach (FeatureAttribute attribute in e.Control.Attributes)
  {
    DataColumnFeatureAttribute columnAttribute = attribute as DataColumnFeatureAttribute;
    if (columnAttribute == null)
      continue;

    if (columnAttribute.ColumnName == "AS_BUILT")
    {
      e.Control.Attributes.Remove(attribute);
      break;
    }
  }
}

void _editAttributesPage_CreatingFeatureAttributes(object sender, EventArgs e)
{
  // Prevent the user from editing the PRESSURE_ZN_ attribute by finding it
  // and removing it from the collection.
  foreach (FeatureAttribute attribute in _editAttributesPage.Attributes)
  {
    DataColumnFeatureAttribute columnAttribute = attribute as DataColumnFeatureAttribute;
    if (columnAttribute == null)
      continue;

    if (columnAttribute.ColumnName == "PRESSURE_ZN_")
    {
      _editAttributesPage.Attributes.Remove(attribute);
      break;
    }
  }
}

9/20/2011