Pages

Wednesday, June 6, 2012

Binding to a Domain’s Coded Values in a Geodatabase (Silverlight and WPF)


This post discusses how Silverlight and WPF developers can utilize coded values for a domain defined on attribute fields in geodatabase for display at runtime (e.g. in MapTips, DataGrids, etc.).  Information about a feature layer can be used to discover coded values established via a domain applied to a field.  While feature attributes contain actual data values, coded values are often used to more effectively describe and present attribute information. For example, actual data values of 1, 2, and 3 may represent New, Open, and Closed.  A domain can be used to establish more descriptive values (New, Open, Closed) for codes (1.2,3) which changing the underlying data structure.  In the ArcGIS APIs for Silverlight and WPF, controls such as the FeatureDataGrid and the FeatureDataForm convert actual data values into coded values for you at runtime.  The APIs also provide developers with a couple of utility classes to convert actual values to coded values for use in other controls or workflows. 
The example below illustrates how to utilize coded values for a field in a feature layer using the CodedValueSources and CodedValueDomainConverter classes included in the ArcGIS APIs Toolkit library (ESRI.ArcGIS.Client.Toolkit.dll).  The coded values are displayed in map tips enabled on a feature layer. In order to get started, first add references to these classes in XAML as resources: 

    
    

The CodedValueSources instance represents a list of codes and values (descriptions) which can be populated upon feature layer initialization.
public partial class MainPage : UserControl
{
    CodedValueSources codedValueSourcesStatusField;

    public MainPage()
    {
        InitializeComponent();

        codedValueSourcesStatusField = LayoutRoot.Resources["codedValueSourcesStatusField"] as CodedValueSources;
    }

    private void FeatureLayer_Initialized(object sender, EventArgs e)
    {
        FeatureLayer flayer = sender as FeatureLayer;
        foreach (Field field in flayer.LayerInfo.Fields)
        {
            if (field.Name == "status")
            {
                CodedValueDomain codedValueDomain = field.Domain as CodedValueDomain;

                foreach (KeyValuePair codeVal in
                    codedValueDomain.CodedValues)
                    codedValueSourcesStatusField.Add(new CodedValueSource()
                    {
                        Code = codeVal.Key,
                        Name = codeVal.Value == null ? "" : codeVal.Value
                    });
                break;
            }
        }
    }
}
And finally, establish a binding to the attribute field on which a domain’s coded values should be used. The CodedValueDomainConverter will convert actual data values to coded values present in the CodedValueSources instance. 

    
        
            
                
                
                    
                    
                
            
        
    

No comments:

Post a Comment