Hi I am trying to use control button to select and unselect features.could not get un

1154
2
03-09-2011 04:56 AM
MuralidharMoka
New Contributor II
Hi,
I wanted to achieve all the functionality of selection/Unselection of feature by holding ctrl key.
Just like how we do for files in windows explorer. I got every thing but could not get the
Unselection of the graphic, when I click on the selected graphic. Can any one help me on this.
Here is my completed code both XAML and code behind.

XAML code:
==================
<UserControl x:Class="SilverlightApplication1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:esri="clr-namespace:ESRI.ArcGIS.Client;assembly=ESRI.ArcGIS.Client"
    xmlns:esrisymbols="clr-namespace:ESRI.ArcGIS.Client.Symbols;assembly=ESRI.ArcGIS.Client"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.Resources>
            <esrisymbols:FillSymbol Fill="Blue" x:Name="BlueFill" />
            <esrisymbols:FillSymbol Fill="MistyRose" x:Name="MistyRose" />
            <esrisymbols:FillSymbol Fill="OrangeRed" x:Name="OrangeRed" />
        </Grid.Resources>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="50" />

        </Grid.RowDefinitions>
        <esri:Map Grid.Row="0" x:Name="MyMap" >
            <esri:ArcGISDynamicMapServiceLayer ID="DynamicLayer" Opacity="0.6"
                Url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapSe..."/>
            <esri:GraphicsLayer ID="graphicLayer" MouseLeftButtonDown="GraphicsLayer_MouseLeftButtonDown"/>
        </esri:Map>
    </Grid>
</UserControl>

==================
XAML code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;
using ESRI.ArcGIS.Client.Symbols;
using ESRI.ArcGIS.Client.Tasks;

namespace SilverlightApplication1
{
    public partial class MainPage : UserControl
    {
        Draw drawobj = null;
        private GraphicsLayer graphiclayer = null;
        private Boolean isCtrKeyPressed = false;
        QueryTask _queryTask;

        public MainPage()
        {
            InitializeComponent();
            MyMap.KeyDown += new KeyEventHandler(MyMap_KeyDown);
            MyMap.KeyUp += new KeyEventHandler(MyMap_KeyUp);
            LayoutRoot.MouseLeftButtonDown += new MouseButtonEventHandler(LayoutRoot_MouseLeftButtonDown);
            drawobj = new Draw(MyMap) { };
            drawobj.DrawMode = DrawMode.Point;
            drawobj.IsEnabled = false;       
         
            drawobj.DrawComplete += new EventHandler<DrawEventArgs>(drawobj_DrawComplete);
        }

        void LayoutRoot_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (graphiclayer != null)
            {
                if(isCtrKeyPressed ==false)
                {
                    if (graphiclayer.Count() > 0)
                    {
                        graphiclayer.ClearGraphics();
                    }
                }
              
            }
           
        }

        void MyMap_KeyUp(object sender, KeyEventArgs e)
        {
            drawobj.IsEnabled = false;
            isCtrKeyPressed = false;
        }

        void MyMap_KeyDown(object sender, KeyEventArgs e)
        {
            if(isCtrKeyPressed)
            {
                return;
            }
            if(e.Key==Key.Ctrl)
            {
                drawobj.IsEnabled = true;
                isCtrKeyPressed = true;
            }
        }


        void drawobj_DrawComplete(object sender, DrawEventArgs e)
        {  
            QueryTask queryTask = new QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/" +
               "Demographics/ESRI_Census_USA/MapServer/5");
            queryTask.ExecuteCompleted += new EventHandler<QueryEventArgs>(queryTask_ExecuteCompleted);
            Query query = new Query();
            query.Geometry = e.Geometry;
            query.ReturnGeometry = true;
            queryTask.ExecuteAsync(query);
        }

     

        void queryTask_ExecuteCompleted(object sender, QueryEventArgs e)
        {
            graphiclayer = MyMap.Layers["graphicLayer"] as GraphicsLayer;
            if (e.FeatureSet != null && e.FeatureSet.Features.Count < 1)
            { return; }

            foreach (Graphic feature in e.FeatureSet.Features)
            {
               
                    feature.Symbol = OrangeRed;
                    graphiclayer.Graphics.Add(feature);           
               
            }

        }

        private void Reset_Click(object sender, RoutedEventArgs e)
        {
            drawobj.IsEnabled = false;
            graphiclayer.ClearGraphics();
        }

   
        private void GraphicsLayer_MouseLeftButtonDown(object sender, GraphicMouseButtonEventArgs e)
        {
            e.Graphic.UnSelect();

        }

        }
}
==========================================
0 Kudos
2 Replies
DominiqueBroux
Esri Frequent Contributor
I don't see any code selecting the graphics!

Check also your symbology. To make a difference between a selected graphic and a unselected graphic, your symbols must react to the 'SelectionStates'.

See sample : http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#SelectGraphics
0 Kudos
JenniferNery
Esri Regular Contributor
You can also look at the following SDK sample: http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#FeatureLayerSelection

The Keyboard button distinguishes selection base on key press. Ctrl key for adding to selection, Shift key for removing from selection and any or no key for new selection.

You can also change this selection behavior on key press by subscribing to KeyDown event and activating Select command programmatically.
void FeatureLayerSelection_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
 if (e.Key == System.Windows.Input.Key.S)
 {
  Editor editor = this.LayoutRoot.Resources["MyEditor"] as Editor;
  if (editor != null && editor.Select.CanExecute("new"))
   editor.Select.Execute("new");
 }
}
0 Kudos