Select to view content in your preferred language

FeatureDataGrid Binding Issue

918
11
11-04-2010 01:37 PM
KeithGanzenmuller
Occasional Contributor II
Having a bear of a time figuring this one out so I am going to post some of the code to see if there is something obvious I am doing wrong. In theory this should be fairly straight forward following the online samples.
Query for some features, display the selected graphics and populate the feature datagrid.

Here is the beginning of some code defining and executing a query task part way down in a Sub:

myGraphicsLayer.ClearGraphics()
SelectionText.Text = _toolBar

Dim queryTask As New Tasks.QueryTask("http://sql2k5/ArcGIS/rest/services/CityBasic/MapServer/8")
AddHandler queryTask.ExecuteCompleted, AddressOf QueryTask_ExecuteCompleted
AddHandler queryTask.Failed, AddressOf QueryTask_Failed

'Code Section 1
Dim resultFeaturesBinding As New Binding("LastResult.Features")
resultFeaturesBinding.Source = queryTask
featureGrid1.SetBinding(DataGrid.ItemsSourceProperty, resultFeaturesBinding)
'End Section 1

Dim query As Query = New ESRI.ArcGIS.Client.Tasks.Query()

' Specify fields to return from query
query.OutFields.AddRange(New String() {"SHAPE", "PARCEL", "PRPDNO", "PRPDST", "OWNERS"})
query.Geometry = args.Geometry
query.ReturnGeometry = True
queryTask.ExecuteAsync(query)
End Sub

'NEW SUB
Private Sub QueryTask_ExecuteCompleted(ByVal sender As Object, ByVal args As ESRI.ArcGIS.Client.Tasks.QueryEventArgs)
MyDrawObject.IsEnabled = True


Dim featureSet As FeatureSet = args.FeatureSet


If featureSet Is Nothing OrElse featureSet.Features.Count < 1 Then
MessageBox.Show("No features returned from query")
Return
End If

For Each feature As Graphic In featureSet.Features
feature.Symbol = TryCast(LayoutRoot.Resources("PolyFillSymbol"), Symbol)

myGraphicsLayer.Graphics.Add(feature)
Next feature


'Code Section 2 '

featureGrid1.ItemsSource = featureSet.Features
'End Section 2

_toolBar = " "


End Sub

So here is the problem, if I include the binding code in Section1, then I get an error in my for each loop at "myGraphicsLayer.Graphics.Add(feature)", inside the error details it mentions System.Reflection.TargetException: Object does not match target type. If comment out the binding code, my graphics add to map but no info into the featuredatagrid. If I add the line at Section 2 "featureGrid1.ItemsSource = featureSet.Features" & comment out Section 1, I get the graphics and the featuredatagrid info, but it crashes if I try to use Zoom to Select.
The real kicker, if I comment out both Section 1 & 2, I get the graphics and the grid shows lines but they are blank. If I select a blank line, its shows the line selected and will zoom to selected.

Thanks,
Keith
0 Kudos
11 Replies
JenniferNery
Esri Regular Contributor
In your XAML, do you set FeatureDataGrid's Map and GraphicsLayer property? If you already have these two properties set by binding, you do not need code sections 1 & 2. Since in your QueryTask_ExecuteCompleted event handler, you already add the features to your GraphicsLayer. Since your Query's OutFields have values, I do not see why the FeatureDataGrid appears blank, you must've missed to set Map="{Binding ElementName=MyMap}" where MyMap is the name of your map.
0 Kudos
KeithGanzenmuller
Occasional Contributor II
I'm sure you are right Jennifer but for the life of me I can't figure out what I am doing wrong with the binding.

Here are some snapshots of my XAML as it sits right now, at the moment, I can select from the Parcels layer and the graphics will appear correctly in the map, nothing at all happens to the featuredatagrid though. In the code behind I commented out Code Sections 1 & 2. Is there something more to it then going to the Map Property, choosing Binding then Element Name = myMap then for Graphicslayer binding I chose Elementname as Parcels, which is the name defined in XAML for a Featurelayer?

<esri:Map x:Name="myMap" IsLogoVisible="False" VerticalContentAlignment="Center">
<esri:Map.Extent>
<esriGeometry:Envelope XMin="1396056" YMin="154236" XMax="1443794" YMax="206608">
<esriGeometry:Envelope.SpatialReference>
<esriGeometry:SpatialReference WKID="3419" />
</esriGeometry:Envelope.SpatialReference>
</esriGeometry:Envelope>
</esri:Map.Extent>
<esri:ArcGISTiledMapServiceLayer ID="TiledAerial" Url="http://sql2k5:80/ArcGIS/rest/services/AerialPhotos/MapServer" x:Name="Aerial" Visible="False" />
<esri:ArcGISDynamicMapServiceLayer ID="CityBasic" Url="http://sql2k5:80/ArcGIS/rest/services/CityBasic/MapServer" x:Name="CityBasic"/>
<esri:ArcGISDynamicMapServiceLayer ID="Violations" Url="http://sql2k5:80/ArcGIS/rest/services/Violations/MapServer" x:Name="Violations" />
<esri:FeatureLayer x:Name="Parcels" ID="Parcels" Url="http://sql2k5/ArcGIS/rest/services/CityBasic/MapServer/8" Mode="SelectionOnly" FeatureSymbol="{StaticResource SelectSymbol}" />
<esri:ArcGISDynamicMapServiceLayer x:Name="CityInfoGroup" ID="CityInfoGroup" Url="http://sql2k5:80/arcgis/rest/services/CityInfoGroup/MapServer" Visible="False" />
</esri:Map>


<esriToolkit:FeatureDataGrid x:Name="featureGrid1" Map="{Binding ElementName=myMap}" GraphicsLayer="{Binding ElementName=Parcels}">
<!--<esriToolkit:FeatureDataGrid.Columns >
<slData:DataGridTextColumn CanUserSort="True" SortMemberPath="PARCEL" Binding="{Binding Attributes[PARCEL]}" Header="Parcel No." />
<slData:DataGridTextColumn CanUserSort="True" Binding="{Binding Attributes[PRPDNO]}" Header="Address" />
<slData:DataGridTextColumn CanUserSort="True" Binding="{Binding Attributes[PRPDST]}" Header="Street" />
<slData:DataGridTextColumn CanUserSort="True" Binding="{Binding Attributes[OWNERS]}" Header="Owners" />
</esriToolkit:FeatureDataGrid.Columns>--> </esriToolkit:FeatureDataGrid>
0 Kudos
AliMirzabeigi
New Contributor
Keith,

Your element binding to the GraphicsLayer property of the FeatureDataGrid should be as the following (your binding to Map property is correct):
GraphicsLayer="{Binding Path=Layers[Parcels], ElementName=myMap}"
Also, FeatureDataGrid automatically populates columns for you unless you explicitly set its AutoGenerateColumns property to FALSE. I suggest do the above with the default behavior (AutoGenerateColumns = true) and see if it works then try to set your own column collections with their corresponding alias headers.
0 Kudos
KeithGanzenmuller
Occasional Contributor II
Thanks Ali.
I commented out the row definitions and entered the binding you suggested but I get nothing in the featuredatagrid unless I have the following in code:

            Dim resultFeaturesBinding As New Binding("LastResult.Features")
            resultFeaturesBinding.Source = queryTask
            featureGrid1.SetBinding(DataGrid.ItemsSourceProperty, resultFeaturesBinding)

If I include those lines of code I get the features in the map and the rows in the featuredatagrid. I get columns for Geometry, Symbol, Attributes and a few others, including a column titled Selected with checkboxes which can't be checked. There is also no selection behavior in the grid, if I click a row it still shows 0 of 0.
Obviously there is something wrong with what I am doing. Perhaps I should start over.
Is there a tutorial which goes step by step through the featuredatagrid to graphics layer process, other than the interactive samples?

Thanks to you and Jennifer for your help.
0 Kudos
AliMirzabeigi
New Contributor
The easiest way is to comment out what you have in your code-behind, then try and remove the "SelectionOnly" mode from your FeatureLayer (The default mode is "Snapshot"), i.e.:
<esri:FeatureLayer ID="Parcels" OutFields="*"  Url="http://sql2k5/ArcGIS/rest/services/CityBasic/MapServer/8"  FeatureSymbol="{StaticResource SelectSymbol}"   />

[I guess you were also missing the "OutFields" property]

Then, use the following code for your FeatureDataGrid:
<esriToolkit:FeatureDataGrid x:Name="featureGrid1" Map="{Binding  ElementName=myMap}" GraphicsLayer="{Binding Path=Layers[Parcels], ElementName=myMap}" />
FeatureDataGrid should be populated by the features you have in your layer as it takes care of the CollectionChanged property of your layer's Graphics object.
0 Kudos
KeithGanzenmuller
Occasional Contributor II
Ok, so here is some weird behavior.
If I use the Mode="OnDemand" or "Snapshot" it takes forever and highlights the first 500 parcels and adds them to the datagrid, 500 being the maximum for 9.3.1. There are upwards of 20,000 parcels so it might not be possible to use anything but "Selection Only". The 500 parcels that are hightlighted on initialization do show up in the datagrid though.
I set it back to Selection Only in XAML.
Now when I start up the map, I zoom in and select some parcels, the graphic outlines show up in the map and the feature grid is MOSTLY blank, I have Outfields="*" so it shows all the column headings and its shows the correct number of selected features, I can click in the grid and step through records, also I can zoom to the selected feature.
Only things is, no actual textual data shows up, except for in one single column out of a couple dozen. So I am clicking on rows but they are blank (except for the one column) even though they exhibit the correct behavior and number of records.
0 Kudos
JenniferNery
Esri Regular Contributor
Since the FeatureLayer is SelectionOnly, at the application startup no features are displayed on the map, no rows are displayed on the FeatureDataGrid. Once a selection is made, in this sample I use the Editor Select command, features get added to the layer and the rows get populated.

I could not replicate the issue with the following XAML-code:
    xmlns:esri="http://schemas.esri.com/arcgis/client/2009">

    <Grid x:Name="LayoutRoot" Background="White">
  <Grid.Resources>
   <esri:Editor x:Key="MyEditor" Map="{Binding ElementName=MyMap}" SelectionMode="Rectangle" />
  </Grid.Resources>
  <esri:Map x:Name="MyMap" Extent="-102.049995692435,  36.995429186312, -94.6031396158311, 40.0025826914111">
   <esri:ArcGISTiledMapServiceLayer ID="Tiled" Url="http://services.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
   <esri:FeatureLayer ID="Parcel" Mode="SelectionOnly"
          OutFields="*"
          Url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Petroleum/KGS_OilGasFields_Kansas/MapServer/0">
    <esri:FeatureLayer.Renderer>
     <esri:SimpleRenderer>
      <esri:SimpleFillSymbol Fill="Blue" />
     </esri:SimpleRenderer>
    </esri:FeatureLayer.Renderer>
   </esri:FeatureLayer>
  </esri:Map>
  <StackPanel VerticalAlignment="Bottom" HorizontalAlignment="Center">
   <Button Content="Select" DataContext="{StaticResource MyEditor}" Command="{Binding Select}"/>
  <esri:FeatureDataGrid x:Name="MyFDG" Map="{Binding ElementName=MyMap}" GraphicsLayer="{Binding ElementName=MyMap, Path=Layers[Parcel]}"  Height="200"/>
  </StackPanel>
 </Grid>


Can you share some code? I'm using a layer from sampleserver1, which is still v9.31.
0 Kudos
AliMirzabeigi
New Contributor
Yes, the way Jennifer presented above is querying against the same layer which might be different than your approach, i.e. using QueryTask. What is happening in your code I guess is that the Graphic instances returned by the FeatureSet's Feature collection are not the same as what you bounded to your datagrid and your layer still contains no graphic. I would definitely give Jennifer's code a try as the Editor control in her code is populating graphics in the layer and FeatureDataGrid respectively.
0 Kudos
KeithGanzenmuller
Occasional Contributor II
Thanks to both you, I appreciate all your help.
I can definitely try the code in the morning, as well as send any of my code that you would like to see.
In the meantime, any ideas why one single column would show values in an otherwise empty grid?

Keith
0 Kudos