Select to view content in your preferred language

Show identify in a dataform

1499
13
03-09-2011 11:54 AM
DonFreeman
Emerging Contributor
I would like to present the result of an identify query in a dataform rather than a datagrid so that I can control the layout and formatting. Does anyone have a sample of how this would be constructed?
Thanks
0 Kudos
13 Replies
JenniferNery
Esri Regular Contributor
You can use your own UserControl for this and set its DataContext to IdentifyResults.Feature.Attributes. http://help.arcgis.com/en/webapi/silverlight/apiref/ESRI.ArcGIS.Client~ESRI.ArcGIS.Client.Tasks.Iden...

Your UserControl can include TextBlocks and/or TextBoxes with Text="{Binding Field}".
0 Kudos
DonFreeman
Emerging Contributor
Thanks Jenn. That would make sense except when I place the bolded line into a block of working code it objects to Feature and says there is no definition for feature in IdentifyResults.
 private void IdentifyTask_ExecuteCompleted(object sender, IdentifyEventArgs args)
   {
   IdentifyDetailsDataGrid.ItemsSource = null;

   if (args.IdentifyResults != null && args.IdentifyResults.Count > 0)
    {
    IdentifyResultsPanel.Visibility = Visibility.Visible;
    IdentifyDataForm2.DataContext = args.IdentifyResults.Feature.Attributes ;    ShowFeatures(args.IdentifyResults);
    }
   else
    {
    IdentifyComboBox.Items.Clear();
    IdentifyComboBox.UpdateLayout();
    IdentifyResultsPanel.Visibility = Visibility.Collapsed;
    }
   }
0 Kudos
DonFreeman
Emerging Contributor
Jenn -
I can create a connection to the data with this line
IdentifyDataForm2.ItemsSource = feature.Attributes; 
but the attribute data appears as a list of name/value pairs and so the dataform only displays the first pair. Is there a way to get the all the attributes together so they will all show in the dataform?
0 Kudos
JenniferNery
Esri Regular Contributor
This sample http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#Identify assumes that the IdentifyComboBox will contain titles and IdentifyDetailsDataGrid will contain the feature attributes. This sample uses DataItem class to update the IdentifyDetailsDataGrid source on IdentifyComboBox SelectionChanged event.

You need to update the XAML-code for IdentifyDetailsDataGrid and replace it with your own data form. You also need to know which layer the feature belongs to in order to know the fields you need for Binding.
For example:
    <StackPanel Orientation="Horizontal">
             <TextBlock Text="State Name: "/>
             <TextBlock Text="{Binding [STATE_NAME]}"/>
    </StackPanel>
0 Kudos
DonFreeman
Emerging Contributor
This sample http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#Identify assumes that the IdentifyComboBox will contain titles and IdentifyDetailsDataGrid will contain the feature attributes. This sample uses DataItem class to update the IdentifyDetailsDataGrid source on IdentifyComboBox SelectionChanged event.

You need to update the XAML-code for IdentifyDetailsDataGrid and replace it with your own data form. You also need to know which layer the feature belongs to in order to know the fields you need for Binding.
For example:
    <StackPanel Orientation="Horizontal">
             <TextBlock Text="State Name: "/>
             <TextBlock Text="{Binding [STATE_NAME]}"/>
    </StackPanel>


Thanks Jenn. For purposes of development and debugging (trying to figure this out) I have both datagrid and dataform on the the page. The datagrid is setup per the esri sample and it works OK. The dataform looks like this.
   <toolkit:DataForm x:Name="IdentifyDataForm2" 
     AutoGenerateFields="False"
     IsReadOnly="True"
     IsEnabled="True"
     LabelPosition="Left"
     Height="150"
     VerticalAlignment="Top">
     <StackPanel Orientation="Horizontal">
      <TextBlock Text="Name: "/>
      <TextBlock Text="{Binding [ST_NAME]}"/>
     </StackPanel>
    </toolkit:DataForm>

The data is triggered by the combobox in the sample.
void cb_SelectionChanged(object sender, SelectionChangedEventArgs e)
   {
   int index = IdentifyComboBox.SelectedIndex;
   if (index > -1)
    IdentifyDetailsDataGrid.ItemsSource = _dataItems[index].Data;
    IdentifyDataForm2.ItemsSource = _dataItems[index].Data ;   ////////////////////////////////

   }
Note both controls are looking at the same data. However while the datagrid displays properly, the dataform displays only the textblock containing the label and not the one containing the data.
0 Kudos
JenniferNery
Esri Regular Contributor
Have you looked at this SL4 Toolkit sample? http://www.silverlight.net/content/samples/sl4/toolkitcontrolsamples/run/default.html. Look at the Template-Driven sample. I think you need to define template for each layer since they will have different fields.

A simpler example will be this. Add the following XAML-code to the Identify SDK sample:
<StackPanel x:Name="MyDataForm" VerticalAlignment="Top" HorizontalAlignment="Center" Orientation="Horizontal">
 <TextBlock Text="State Name: "/>
 <TextBlock Text="{Binding [STATE_NAME]}"/>
</StackPanel>


In code-behind, set its DataContext when DataGrid gets its ItemsSource.
void cb_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
 int index = IdentifyComboBox.SelectedIndex;
 if (index > -1)
 {
  IdentifyDetailsDataGrid.ItemsSource = _dataItems[index].Data;
  MyDataForm.DataContext = _dataItems[index].Data;
 }
}
0 Kudos
DonFreeman
Emerging Contributor
Have you looked at this SL4 Toolkit sample?   http://www.silverlight.net/content/samples/sl4/toolkitcontrolsamples/run/default.html. Look at the Template-Driven sample. I think you need to define template for each layer since they will have different fields. 

A simpler example will be this. Add the following XAML-code to the Identify SDK sample: 
<StackPanel x:Name="MyDataForm" VerticalAlignment="Top" HorizontalAlignment="Center" Orientation="Horizontal">
 <TextBlock Text="State Name: "/>
 <TextBlock Text="{Binding [STATE_NAME]}"/>
</StackPanel>


In code-behind, set its DataContext when DataGrid gets its ItemsSource. 
void cb_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
 int index = IdentifyComboBox.SelectedIndex;
 if (index > -1)
 {
  IdentifyDetailsDataGrid.ItemsSource = _dataItems[index].Data;
  MyDataForm.DataContext = _dataItems[index].Data;
 }
}


Hmmm... I guess maybe I need an explanation of the difference between DataForm.DataContext and DataForm.ItemsSource. In the sample the xaml refers to ItemsSource but the codebehind refers to DataContext. If I set the dataform to autogenerate the fields and codebehind to set the DataContext as shown in the sample, I get both label and data for the first field only (OBJECTID_1).

If I set it up as you suggest using a bound textblock in the xaml (which is what I want) and DataContext in the codebehind, I get nothing in the output. If I then change the codebehind to set the ItemsSource, I get the labels but not the data. So the whole thing remains a puzzle.
0 Kudos
DonFreeman
Emerging Contributor
I can make a FeatureDataForm work but it looks like I cannot control its contents since it will not accept an AutoGenerateFields setting. Is that true?
0 Kudos
JenniferNery
Esri Regular Contributor
You are using Silverlight Data Form now, right? This is not the same as FeatureDataForm from our Toolkit. The fields in the FeatureDataForm come from the FeatureLayer.OutFields. How they are displayed on the form is dependent on the data type defined by the service.
0 Kudos