Select to view content in your preferred language

SpatialQueryAction & dataGrid

2961
27
06-21-2012 05:58 AM
MiriEshel
Esri Contributor
Hi,

Is there a way to show a DataGrid of the selected features using SpatialQueryAction.

There is a sample called: http://resourcesbeta.arcgis.com/en/help/silverlight-api/samples/start.htm#GraphicsActions
I want to extend this sample to show also the dataGrid of the selection's result.

Thanks a lot,
Miri
0 Kudos
27 Replies
JoeHershman
MVP Alum


I admire your patience....

Thanks,
Miri


Toda Raba 🙂


In Silverlight the ah ha moment is when the data binding thing actually makes sense.  The DataContext is pretty much a weakly typed object that any control can have attached to it.  DataContext is inherited, so if I set the Context at the UserControl level everything shares that DataContext unless the control overrides it. 

This Xaml

    <actions:SetLayerUrlAction Url="{Binding _ActiveLayer.URL}"/>



Is saying look at my DataContext (some object) for a (public) Property called _ActiveLayer and then attach to the Url property.  But you have not defined this DataContext so it does not know what to look at.  In the Button code it is saying the Map is DataContext and will look for a property on Map (which does not exist) 

So the DataContext of the page needs to be set so the Binding knows what it should look for (I am not sure if setting on the button works or if it needs to be the page)


        public MainPage()
        {
            InitializeComponent();

            DataContext = this;
        }



in the class define your ActiveLayer object (public)


    public ActiveLayer ActiveLayer { get; set; }


Now the MainPage UserControl is bound to the class MainPage class and so

    <actions:SetLayerUrlAction Url="{Binding ActiveLayer.URL}"/>



Knows to look at the ActiveLayer property.

Likewise you could set the DataContext to the ActiveLayer object itself, and then your binding is just to the properties on the ActiveLayer.  This would be a more common approach because you would normally bind to the object that is raising the PropertyChanged events


        public MainPage()
        {
            InitializeComponent();
            ActiveLayer = new ActiveLayer();
            DataContext = ActiveLayer ;
        }
      
         public ActiveLayer ActiveLayer { get; set; }

        <actions:SetLayerUrlAction Url="{Binding URL}"/>


In a statement like {Binding ElementName=MyMap, Path=Layers[MyLayer]} it is setting the DataContext local to that Binding to the specific element.

Good Luck
Thanks,
-Joe
0 Kudos
JoeHershman
MVP Alum
Looking at this some more I realize that the Action would not have a DataContext because it is not a FrameworkElement.  So you would need to set the DataContext on something like the Button and use that to set the property on the Action

So something like this in the XAML


                <Button x:Name="QueryButton" Height="30" Width="30" Margin="0,0,2,0"
                    HorizontalContentAlignment="Left" Padding="2" Content="Query">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="Click">
                            <Actions:SpatialQueryAction
                                    DrawMode="Rectangle"
                                    OutFields="FID,STATE_NAME,STATE_ABBR"
                                    LayerID="MyQueryResultsGraphicsLayer"
                                    Url="{Binding ElementName=QueryButton, Path=DataContext.Url}" 
                                    Symbol="{StaticResource GraphicsLayerFillSymbol}" QueryComplete="SpatialQueryAction_QueryComplete"
                                    TargetName="Map" />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </Button>



And in the code


        public MainPage()
        {
            InitializeComponent();


            ActiveLayer = new ActiveLayer { Url = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/2" };


            QueryButton.DataContext = ActiveLayer;
        }




        public ActiveLayer ActiveLayer { get; set; }


        private void SpatialQueryAction_QueryComplete(object sender, EventArgs e)
        {
        }



And because I seemingly have too much time on my hands, and I thought the idea useful I wrote a SpatialQueryAction that throws a QueryComplete event at the end, which I attached
Thanks,
-Joe
0 Kudos
MiriEshel
Esri Contributor
Hi Joe,

1. Now it is finally works. I wasn't aware of all these DataContext issues....I'm studing and programming at the same time so I miss things.
I haven't tried the symbol yet but I'm sure it will work. Thanks a lot!

2. So sweet of you to write this code on action completion. The thing is that if there is so much code for it, I don't see the advantage of using it instead of QueryTask. Can you see any advantage?

3. How do you know what is: Toda Raba?

4. Do you have a tip/sample for wrapping DataGrid (or featureDataGrid) in a draggable/floatingWindow with close button? I saw some threads about it in the forum but couldn't find someting that attracted me.

Thanks a lot and Shabat Shalom,
Miri
0 Kudos
MiriEshel
Esri Contributor
Hi,

Can I use OnApplyTemplate on the button that executes the spatialQueryAction so it will keep in a gglobal variable the last Tool that was executed?

Thanks,
Miri
0 Kudos
JoeHershman
MVP Alum
Hi Joe,

1. Now it is finally works. I wasn't aware of all these DataContext issues....I'm studing and programming at the same time so I miss things.
I haven't tried the symbol yet but I'm sure it will work. Thanks a lot!



Personally for me, understanding how DataContext works took a bit, it does open up a lot more in Silverlight once it makes sense


2. So sweet of you to write this code on action completion. The thing is that if there is so much code for it, I don't see the advantage of using it instead of QueryTask. Can you see any advantage?


THe advantage and the nice thing about TriggerActions is it is a good way to separate business logic away from the UserControl code behind, really a design decision more than anything else


3. How do you know what is: Toda Raba?


Falls in the things we learned before we were 13 category I guess.;).  I spent one summer in your home back during high school and hope to visit again soon because I am working in the region right now



4. Do you have a tip/sample for wrapping DataGrid (or featureDataGrid) in a draggable/floatingWindow with close button? I saw some threads about it in the forum but couldn't find someting that attracted me.



What I do is create a control that inherits from the DraggableWindow in the Template project and use that to implement that behavior.  I find it a lot cleaner than trying to put everything in the MainPage.  I actually have a sample of a FeatureDataGrid and FeatureDataForm integrated into a DraggableWindow I can post an example later

Good Luck
Thanks,
-Joe
0 Kudos
MiriEshel
Esri Contributor
Hi Joe,

1. I will be happy to receive your sample. I'm also interested in minimizing the draggable window to an icon on the map. I have no clue how to do it.

2. Did you see my last thread asking about onDataTemplate of the button? What do you think?

3. I would love to host you in Israel if you decide to come again. At this moment, I know Israel much more than I know Silverlight. I hope it will change within few weeks....

Thanks a lot,
Miri
0 Kudos
MiriEshel
Esri Contributor
Hi Joe,

1. I've decided to give up on preserving the last tool meanwhile so, at the moment, I don't need the QueryComplete event at the end.
   I do need to check, whether the Url of the active layer is equal to X or equal to None and if the answer is yes, do not execute the spatialQueryAction.
   Is that possible without writing my own spatialQuery?


2. Please send me the sample of a FeatureDataGrid and FeatureDataForm integrated into a DraggableWindow.

I hope I don't nagg too much...

So many thanks,
Miri
0 Kudos
JoeHershman
MVP Alum
Hi Joe,

1. I've decided to give up on preserving the last tool meanwhile so, at the moment, I don't need the QueryComplete event at the end.
   I do need to check, whether the Url of the active layer is equal to X or equal to None and if the answer is yes, do not execute the spatialQueryAction.
   Is that possible without writing my own spatialQuery?


2. Please send me the sample of a FeatureDataGrid and FeatureDataForm integrated into a DraggableWindow.

I hope I don't nagg too much...

So many thanks,
Miri


The RelatedEdt zip is an example of an app that has a DataGrid and DataForm working together and hosted in a DraggableWindow.  There has been something wrong with ESRI's sample server for the last day so I have not finished a couple tiny spots.  This was actually written to edit records in a related table.  The user clicks on a point and if it has related data in the table the DataGrid is loaded.  When a record in the Grid is selected the form is loaded so the record can be edited.  There is an Add button to add a new record in the related table.  I think it should be relatively straight forward to change to just edit the features not related records

I also attached a custom DraggableWindow that does a minimize to just the title bar.  It has a lot of stuff in there that is there as part of the application framework I have developed, so it won't work as is but maybe give some ideas of how to go about it.  I also added some gee-wiz animation to the close.  There is also something you may find interesting in that it can flip from LeftToRight to RightToLeft dynammically

Hope that helps
Thanks,
-Joe
0 Kudos
MiriEshel
Esri Contributor
Hi Joe,

Thanks a lot. I will look at it soon and let you know how it goes.

Thanks again,
Miri
0 Kudos
MiriEshel
Esri Contributor
Hi Joe,

Thanks. I took most of your draggableWindow code and changed it a little bit. It is really compact.

I have two questions:
1. When I do minimize and then maximize, it always returns to the original size (Width="280" Height="180" in this case) even if I resize the window.
Here is the code:
         <userControls:DraggableWindow x:Name="DragWondow" Visibility="{Binding ElementName=Map, Path=Layers[SpatialQueryGraphicsLayer].Graphics.Count, Converter={StaticResource NumOfSelectToVisibilityConverter}}" Width="280" Height="180" Foreground="Black" IsOpen="{Binding Visible, Mode=TwoWay}" Grid.Row="2" >
                <esri:FeatureDataGrid Visibility="{Binding ElementName=Map, Path=Layers[SpatialQueryGraphicsLayer].Graphics.Count, Converter={StaticResource NumOfSelectToVisibilityConverter}}" Grid.Row="2" Grid.Column="1"  x:Name="MyDataGrid" Map="{Binding ElementName=Map}"
                GraphicsLayer="{Binding ElementName=Map, Path=Layers[SpatialQueryGraphicsLayer]}" />
            </userControls:DraggableWindow>


Is there a way to return to the last size of the window (after the last resize). Does it keep this size somewhere?

2. After I close the Window, it does not reopen again even if Layers[SpatialQueryGraphicsLayer].Graphics.Count greater than 0 (visibility=visible). Why?

Thanks a lot,
What would I do without you...
Miri

P.S. Do you know a way to retreive the Layer type (Point, Polyline, Polygon) of an individual layer in a dynamicMapService layer? I thought it will be part of the layerInfo or there will be some method to get it out of the dynamic layer or featureLayer but there isn't. Pretty basic....
0 Kudos