Select to view content in your preferred language

Find and Formatting

2219
25
01-06-2011 11:18 AM
JayKappy
Frequent Contributor
I am trying to format a window after I run the FIND example from the API examples

http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#Find

I have this working but I do not want a grid/scrollviewer to display my results...I want to display them in a more meaningful fashion...

ex
PID: field value
Address: field value

PID: field value
Address: field value

PID: field value
Address: field value

How can I do this...I am doign this but only for the Identify and that is only one record showing...
I cant figure out how to do this in respect to the FIND example and outside a grid...

Any thoughts....

Thanks
0 Kudos
25 Replies
JayKappy
Frequent Contributor
Alright I think I am getting close but would be thankful if anyone had a little comment or two on how to get the ListBox populated.

This is what I am doing...I have a textbox on the app....the user types in a value and clicks a button....a query is done and the in this case the feature is highlighted...

I just cant figure out how to populated the ListBox...I tried with this:
imageList.Items.Add(resultFeature.Attributes("PID").ToString())


IF I ADD THIS: Then I get stuck in a loop with all the PID values being returned one at a time...so I am getting some data back...but if I am getting the data back why is it not showing up in the LISTBOX???

Dim Test2 As String = resultFeature.Attributes("PID").ToString()
MessageBox.Show(Test2)


        If featureSet.Features.Count > 0 Then
            ' Add results to map
            For Each resultFeature As Graphic In featureSet.Features
                resultFeature.Symbol = ResultsFillSymbol
                graphicsLayer.Graphics.Add(resultFeature)

                imageList.Items.Add(resultFeature.Attributes("PID").ToString())

                Dim Test2 As String = resultFeature.Attributes("PID").ToString()
                MessageBox.Show(Test2)

            Next
        Else





XAML:
            <Grid x:Name="resultsPanel"  Width="200" 
     HorizontalAlignment="Left" VerticalAlignment="Top"
     Margin="10,40,0,10">
                <Border Padding="2" Style="{StaticResource darkBorder}">
                    <StackPanel Orientation="Vertical" >
                        <TextBlock x:Name="Status" Text="" Margin="3,0,0,0" Foreground="White" FontWeight="Bold" />
                        <Grid Background="Transparent" >
                            <Button HorizontalAlignment="Left" Style="{StaticResource darkButtonStyle}"
        x:Name="previousButton" Content="&lt;&lt;" Margin="3,0,3,0" IsEnabled="False"  />
                            <Button HorizontalAlignment="Right" Style="{StaticResource darkButtonStyle}"
        x:Name="nextButton" Content="&gt;&gt;" Margin="3,0,3,0" IsEnabled="False"  />
                        </Grid>
                        <ListBox x:Name="imageList" Height="200"  ScrollViewer.VerticalScrollBarVisibility="Visible" 
        BorderThickness="0"
        Foreground="White" Background="Transparent"
        SelectionChanged="imageList2_SelectionChanged">
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <!-- <TextBlock Text="{Binding Path=Title}"  />  -->
                                    <!-- <TextBlock Text="{Binding Path=Title}" MouseEnter="TextBlock_MouseEnter" MouseLeave="TextBlock_MouseLeave" /> -->
                                     <TextBlock Foreground="White" Text="{Binding Feature.Attributes[ADDRESS], StringFormat='Address: \{0\}'}"  /> 
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </StackPanel>
                </Border>
            </Grid>




VB:
    Private Sub ExecuteList_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        ' Query task initialization
        Dim queryTask As New QueryTask("http://gis.org/arcgis/rest/services/MG_Test_WGS84/MapServer/8")
        AddHandler queryTask.ExecuteCompleted, AddressOf QueryTask_ExecuteCompletedListBox
        AddHandler queryTask.Failed, AddressOf QueryTask_FailedSearch

        ' Query task parameters. Return geometry, state, and population density.
        Dim query As New ESRI.ArcGIS.Client.Tasks.Query()
        query.OutFields.Add("*")
        query.Text = FindText.Text
        query.ReturnGeometry = True
        query.OutSpatialReference = MyMap.SpatialReference
        queryTask.ExecuteAsync(query)

    End Sub

    Private Sub QueryTask_ExecuteCompletedListBox(ByVal sender As Object, ByVal args As ESRI.ArcGIS.Client.Tasks.QueryEventArgs)
        ' Clear previous results
        Dim graphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("MyGraphicsLayerSearch2"), GraphicsLayer)
        graphicsLayer.ClearGraphics()

        ' Check for new results 
        Dim featureSet As FeatureSet = args.FeatureSet
        If featureSet.Features.Count > 0 Then
            ' Add results to map
            For Each resultFeature As Graphic In featureSet.Features
                resultFeature.Symbol = ResultsFillSymbol
                graphicsLayer.Graphics.Add(resultFeature)

                imageList.Items.Add(resultFeature.Attributes("PID").ToString())
            Next
        Else
            MessageBox.Show("No features found")
        End If

    End Sub
0 Kudos
JenniferNery
Esri Regular Contributor
You would populate the ListBox items, the same way you would ItemsControl and DataGrid.
            // Bind data grid to find results.  Bind to the LastResult property which returns a list
            // of FindResult instances.  When LastResult is updated, the ItemsSource property on the 
            // will update.  
            Binding resultFeaturesBinding = new Binding("LastResult");
            resultFeaturesBinding.Source = findTask;
            imageList.SetBinding(ListBox.ItemsSourceProperty, resultFeaturesBinding);

Notice that the code above is the same code in post #2 for ItemsControl and in the SDK sample http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#Find for DataGrid, with the exception on the text in red, where imageList is the name of your control and ListBox is the type of your control.

To select the feature based on the ListBox selection, the symbol that you use for the feature can define SelectionStates as Morten pointed you to this sample http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#CustomSymbols, look at SelectRectangleMarkerSymbol where the outline of the rectangle changes from blue to red. In code-behind all that needed to happen is to call Select() or UnSelect() on the graphic.

So in the ListBox's SelectionChanged event - you know that the SelectedItem is a graphic and can therefore call Select() or UnSelect() on it.
0 Kudos
JayKappy
Frequent Contributor
I added that code to the "ExecuteList_Click" and nothing happens....That is while I was using a QueryTask....I could not get it to populate the listbox with this angle, Using the "ExecuteList_Click" and "QueryTask_ExecuteCompletedListBox" sub routines...

I removed the QueryTask and replaced it with FindTask (thus removing the "QueryTask_ExecuteCompletedListBox" and now the Listbox Populates...was I doign soemthign wrong with the QueryTask? 

But without the "QueryTask_ExecuteCompletedListBox" then the graphics are not drawn on the map...
The reason I removed the Binding code as you refered to was that it was in the FIND not the QUERY....I think the listbox gets populated in the "QueryTask_ExecuteCompletedListBox"....Although I did try and put the BINDING code in the "ExecuteList_Click", but nothing happened

Why would it populate with the FindTask and not the QueryTask.
THANK you all for your patience and help....learning everyday here...

What do I have wrong here....NO graphics and No Listbox population

The message box in the "QueryTask_ExecuteCompletedListBox" does return 22, the correct number of records in the layer....so I know the query is sort of working....

    Private Sub ExecuteList_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)

        ' Query task initialization
        Dim queryTask As New QueryTask("http://gis.logis.org/arcgis/rest/services/MG_Test_WGS84/MapServer/1")
        AddHandler queryTask.ExecuteCompleted, AddressOf QueryTask_ExecuteCompletedListBox
        AddHandler queryTask.Failed, AddressOf QueryTask_FailedSearch

        Dim query As New ESRI.ArcGIS.Client.Tasks.Query()
        query.OutFields.Add("*")
        query.ReturnGeometry = True
        ' Return all features
        query.Where = "1=1"
        query.OutSpatialReference = MyMap.SpatialReference
        queryTask.ExecuteAsync(query)
    End Sub

    Private Sub QueryTask_ExecuteCompletedListBox(ByVal sender As Object, ByVal args As ESRI.ArcGIS.Client.Tasks.QueryEventArgs)
        ' Clear previous results
        Dim graphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("MyGraphicsLayerListBox"), GraphicsLayer)
        graphicsLayer.ClearGraphics()

        ' Check for new results 
        Dim featureSet As FeatureSet = args.FeatureSet

        ' Populate image listbox
        imageList.DataContext = featureSet.Features

        ' get feature count
        Dim Test As String = featureSet.Features.Count
        MessageBox.Show(Test)

        If featureSet.Features.Count > 0 Then
            ' Add results to map
            For Each resultFeature As Graphic In featureSet.Features
                resultFeature.Symbol = TryCast(LayoutRoot.Resources("ResultsFillSymbol"), ESRI.ArcGIS.Client.Symbols.Symbol)
                graphicsLayer.Graphics.Add(resultFeature)
            Next
        Else
            MessageBox.Show("No features found")
        End If
    End Sub
0 Kudos
JenniferNery
Esri Regular Contributor
Looking at your code, you were adding each PID as string to your ListBox in the Query ExecuteCompleted event handler. This means that you will also need to update the XAML-code.

The following snippets from your code-behind and XAML-code do not match:

 imageList.Items.Add(resultFeature.Attributes("PID").ToString())


<TextBlock Foreground="White" Text="{Binding Feature.Attributes[ADDRESS], StringFormat='Address: \{0\}'}"  /> 


The Binding statement here becomes incorrect since the ListBox no longer contains a property Feature with a dictionary of Attributes that has index "ADDRESS" anymore.

To fix this, you can alter your code by adding resultFeature directly to your ListBox or changing the Binding statement in XAML to just Text="{Binding StringFormat='Address: \{0\}'}".
0 Kudos
JayKappy
Frequent Contributor
My bad on the Graphic not showing up....I was using a FILL symbl for a Point...I changed it to Marker Symbopl and wa-la it showed up...in the QUERY.....I do not get a graphic in the FIND example

Again I can get the Listbox to populate with the FIND but my last entry was with the Query.  I would really like to get them both working for my own knowledge....
I just cant see to get the Listbox to populate with the QUERY....I had 3 lines of binding code you gave in there and nothing....I then removed it as my other Query example set a combobox on the QueryTask_ExecuteCompleted sub.

Please reference my last entry in hopes that I can figure out why my listbox is not populating...

THANKS

Working on your last post jennifer....thanks
0 Kudos
JayKappy
Frequent Contributor
Ok that makes sense.....but here is another wrench in the wheel...as I stated earlier I am looking to get multiple attributes return to the listbox for each record...

CityName Field Value
Population Field Value
-------------------------------
CityName Field Value
Population Field Value
-------------------------------
etc

By doign this in the VB
imageList.Items.Add(resultFeature.Attributes("TYPE").ToString())


and this in the xaml
<TextBlock Text="{Binding StringFormat='Type: \{0\}'}" />


I was able to return results...
BUT what happens if I want to do this in the listbox/

<StackPanel Orientation="Horizontal" VerticalAlignment="Center" >
   <StackPanel Margin="1">
       <TextBlock Foreground="White" Text="{Binding StringFormat='City Name: \{0\}'}" />
   </StackPanel>
   <StackPanel Margin="5,0,0,0" Orientation="Vertical">
       <TextBlock Foreground="White" Text="{Binding StringFormat='City Name: \{0\}'}" />
       <TextBlock Foreground="White" Text="{Binding StringFormat='Population: \{0\}'}"  />
   </StackPanel>
</StackPanel>

I tried this and I only got the TYPE in all three TextBlocks in the listbox....I dont see any connection to specifiy which value goes where...
Is there a way to tag?

                ' POPULATE THE LISTBOX
                imageList.Items.Add(resultFeature.Attributes("TYPE").ToString())
                imageList.Items.Add(resultFeature.Attributes("CITY_NAME").ToString())
0 Kudos
JenniferNery
Esri Regular Contributor
What I would do, instead of adding individual attribute value to the ListBox is set the ListBox ItemsSource to the features returned in Query ExecuteCompleted event handler.
imageList.ItemsSource= args.FeatureSet.Features


In doing this, you know that the elements of your ListBox is a collection of features with Attributes. This will allow you to use the previous Binding statements you had where you access the Attribute by their key.

Your ListBox.ItemTemplate DataTemplate could then include the following:
<TextBlock Text="{Binding Feature.Attributes[CITYNAME], StringFormat=CityName: \{0\}'}"  />
<TextBlock Text="{Binding Feature.Attributes[POPULATION], StringFormat=Population: \{0\}'}"  />


The idea is know the type of the elements contained in your ListBox so that you can write the correct Binding statement to access these values. Since you were adding them as strings before, there was no need for "Feature.Attributes[FIELDNAME]" but if you will be adding the resulting features, this - "Feature.Attributes[FIELDNAME]" - is needed.
0 Kudos
JayKappy
Frequent Contributor
This is what I did per your comments....nothing is showing up in the listbox Although the graphics are still showing up....

XAML

                                            
<StackPanel Margin="1">
        <TextBlock Text="{Binding Feature.Attributes[TYPE], StringFormat='City Name: \{0\}'}" />
</StackPanel>
<StackPanel Margin="5,0,0,0" Orientation="Vertical">
         <TextBlock Foreground="White" Text="{Binding Feature.Attributes[CITY_NAME], StringFormat='City Name: \{0\}'}" >
                 <TextBlock.Effect>
                         <DropShadowEffect/>
                 </TextBlock.Effect>
          </TextBlock>
          <TextBlock  Foreground="White" Text="{Binding Feature.Attributes[POP1990], StringFormat='Population: \{0\}'}"  >
                  <TextBlock.Effect>
                         <DropShadowEffect/>
                   </TextBlock.Effect>
           </TextBlock>
</StackPanel>


    Private Sub ExecuteList_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)

        '' Query task initialization
        Dim queryTask As New QueryTask("http://gis.logis.org/arcgis/rest/services/MG_Test_WGS84/MapServer/1")
        AddHandler queryTask.ExecuteCompleted, AddressOf QueryTask_ExecuteCompletedListBox
        AddHandler queryTask.Failed, AddressOf QueryTask_FailedSearch

        '' '' BINDING TO LIST BOX
        Dim resultFeaturesBinding As New Binding("LastResult")
        resultFeaturesBinding.Source = queryTask
        imageList.SetBinding(ListBox.ItemsSourceProperty, resultFeaturesBinding)

        Dim query As New ESRI.ArcGIS.Client.Tasks.Query()
        query.OutFields.Add("*")
        query.ReturnGeometry = True
        ' Return all features
        query.Where = "1=1"
        query.OutSpatialReference = MyMap.SpatialReference
        queryTask.ExecuteAsync(query)
    End Sub

    Private Sub QueryTask_ExecuteCompletedListBox(ByVal sender As Object, ByVal args As ESRI.ArcGIS.Client.Tasks.QueryEventArgs)
        ' Clear previous results
        Dim graphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("MyGraphicsLayerListBox"), GraphicsLayer)
        graphicsLayer.ClearGraphics()

        ' Check for new results 
        Dim featureSet As FeatureSet = args.FeatureSet

        ' Add Items to listbox
        imageList.ItemsSource = args.FeatureSet.Features

        Dim Test As String = featureSet.Features.Count
        MessageBox.Show(Test)

        If featureSet.Features.Count > 0 Then
            ' Add results to map
            For Each resultFeature As Graphic In featureSet.Features
                resultFeature.Symbol = TryCast(LayoutRoot.Resources("DefaultMarkerSymbol"), ESRI.ArcGIS.Client.Symbols.Symbol)
                graphicsLayer.Graphics.Add(resultFeature)
            Next
        Else
            MessageBox.Show("No features found")
        End If
    End Sub
0 Kudos
JenniferNery
Esri Regular Contributor
Oh I'm sorry I got it confused with the FindTask solution.

Remove "Feature." in the Binding statements.
Text="{Binding Attributes[CITY_NAME], StringFormat='City Name: \{0\}'}"

Also, in your code-behind you don't need these lines anymore, if you are setting ItemsSource in the ExecuteCompleted.
        '' '' BINDING TO LIST BOX
        Dim resultFeaturesBinding As New Binding("LastResult")
        resultFeaturesBinding.Source = queryTask
        imageList.SetBinding(ListBox.ItemsSourceProperty, resultFeaturesBinding)


To clarify:
In the three solutions posted in this thread, no one solution is more correct than the other. They all work fine. You just need to tweak the Binding statements depending on the content of your ListBox.

Solution 1: Bind ItemsSourceProperty as in the FindTask example
        Dim resultFeaturesBinding As New Binding("LastResult")
        resultFeaturesBinding.Source = queryTask
        imageList.SetBinding(ListBox.ItemsSourceProperty, resultFeaturesBinding)

Correct Binding statement is - Text="{Binding Feature.Attributes[CITY_NAME], StringFormat='City Name: \{0\}'}"

Solution 2: Set ItemsSource in the Query ExecuteCompleted event handler.
       imageList.ItemsSource = args.FeatureSet.Features

Correct Binding statement is - Text="{Binding Attributes[CITY_NAME], StringFormat='City Name: \{0\}'}"

Solution 3: Add Items as string in the Query ExecuteCompleted event handler
        imageList.Items.Add(resultFeature.Attributes("CITY_NAME").ToString())

Correct Binding statement is - Text="{Binding StringFormat='City Name: \{0\}'}"
0 Kudos
JayKappy
Frequent Contributor
AWESOME!!!!!

Thats what I was looking for....I was getting really confused on the 2-3 different approaches I was taking....then getting caught up in the binding statements...

How you explained it makes perfect sense...I look foward to passing on what I have learned....

ANYONE reading this have any questions please feel free to ask
jaykappy@yahoo.com

Thank you very much.....NOW on to Selecting and Unselecting to make them highlight on hover or MouseEnter in the listbox....
Think I have to Dim the Graphic adn then simply say Graphic.Select or something....will look into that...

THANKS AGAIN...ALL YOUR HELP IS VERY VERY APPRECIATED....
0 Kudos