Select to view content in your preferred language

Draw Graphics Tool

3734
3
06-20-2012 12:25 PM
ScottBailey
Deactivated User
I have converted the DrawGraphics tool  http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#DrawGraphics to a Viewer addin. Everything works fine except for the editor check box. I have tried a ton of different things but the edit geometry box is not functional. It has something to do with the MouseLeftButtonUp event. I'm not sure whats going on. Code is below.

C#
namespace DrawGraphics.AddIns
{
    public partial class DrawGraphicsDialog : UserControl
    {
        private Draw MyDrawObject;
        private Symbol _activeSymbol = null;
        GraphicsLayer graphicsLayer;

        public Map MyMap
        {
            get { return MapApplication.Current.Map; }
        }

       
        public GraphicsLayer MyGraphicsLayer
        {
            get { return MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer; }
        }

        public DrawGraphicsDialog()
        {
            InitializeComponent();

            graphicsLayer = MyGraphicsLayer;

            if (graphicsLayer == null)
            {
                graphicsLayer = createGraphicsLayer();
                MapApplication.Current.Map.Layers.Add(graphicsLayer);
            }

            MyDrawObject = new Draw(MyMap)
            {
                LineSymbol = LayoutRoot.Resources["DrawLineSymbol"] as LineSymbol,
                FillSymbol = LayoutRoot.Resources["DrawFillSymbol"] as FillSymbol
            };
            MyDrawObject.DrawComplete += MyDrawObject_DrawComplete;
        }

        private void MyDrawObject_DrawComplete(object sender, ESRI.ArcGIS.Client.DrawEventArgs args)
        {
           
           
            ESRI.ArcGIS.Client.Graphic graphic = new ESRI.ArcGIS.Client.Graphic()
            {
                Geometry = args.Geometry,
                Symbol = _activeSymbol,
            };
            graphicsLayer.Graphics.Add(graphic);
            UnSelectTools();
            MyDrawObject.IsEnabled = false;
        }



        private void GraphicsLayer_MouseLeftButtonUp(object sender, GraphicMouseButtonEventArgs e)
        {
            if (EnableEditVerticesScaleRotate.IsChecked.Value)
            {
                MyDrawObject.DrawMode = DrawMode.None;
                UnSelectTools();
                Editor editor = LayoutRoot.Resources["MyEditor"] as Editor;
                if (e.Graphic != null && !(e.Graphic.Geometry is ESRI.ArcGIS.Client.Geometry.MapPoint))
                {
                    editor.EditVertices.Execute(e.Graphic);
                }
            }
        }

        private void UnSelectTools()
        {
            foreach (UIElement element in MyStackPanel.Children)
                if (element is Button)
                    VisualStateManager.GoToState((element as Button), "UnSelected", false);
        }

        private void Tool_Click(object sender, RoutedEventArgs e)
        {
            UnSelectTools();

            VisualStateManager.GoToState(sender as Button, "Selected", false);

            switch ((sender as Button).Tag as string)
            {
                case "DrawPoint":
                    MyDrawObject.DrawMode = DrawMode.Point;
                    _activeSymbol = LayoutRoot.Resources["DefaultMarkerSymbol"] as Symbol;
                    break;
                case "DrawPolyline":
                    MyDrawObject.DrawMode = DrawMode.Polyline;
                    _activeSymbol = LayoutRoot.Resources["DefaultLineSymbol"] as Symbol;
                    break;
                case "DrawPolygon":
                    MyDrawObject.DrawMode = DrawMode.Polygon;
                    _activeSymbol = LayoutRoot.Resources["DefaultFillSymbol"] as Symbol;
                    break;
               
                default:
                    MyDrawObject.DrawMode = DrawMode.None;
                    graphicsLayer.ClearGraphics();
                    break;
            }
           
            MyDrawObject.IsEnabled = (MyDrawObject.DrawMode != DrawMode.None);
        }

       private GraphicsLayer createGraphicsLayer()
        {
            GraphicsLayer gl = new GraphicsLayer()
            {
                ID = "MyGraphicsLayer",
               
               
            };

            // Set layer name in Map Contents
            gl.SetValue(MapApplication.LayerNameProperty, "MyGraphicsLayer");
            return gl;
        }

       
    }
}






XAML



<UserControl x:Class="DrawGraphics.AddIns.DrawGraphicsDialog"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:esri="http://schemas.esri.com/arcgis/client/2009" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="118" d:DesignWidth="571" >
    <Grid x:Name="LayoutRoot" Background="White" Width="566" Height="107">

        <Grid.Resources>

            <esri:SimpleLineSymbol x:Key="DrawLineSymbol" Color="Green" Width="4" />
            <esri:SimpleFillSymbol x:Key="DrawFillSymbol" Fill="#3300FF00" BorderBrush="Green" BorderThickness="2" />
            <esri:SimpleMarkerSymbol x:Key="DefaultMarkerSymbol" Color="Red" Size="12" Style="Circle" />
            <esri:SimpleLineSymbol x:Key="DefaultLineSymbol" Color="Red" Width="4" />
            <esri:SimpleFillSymbol x:Key="DefaultFillSymbol" Fill="#33FF0000" BorderBrush="Red" BorderThickness="2" />
            <esri:GraphicsLayer x:Key="MyGraphicsLayer" ID="MyGraphicsLayer" MouseLeftButtonUp="GraphicsLayer_MouseLeftButtonUp" />
            <esri:Editor x:Key="MyEditor" Map="{Binding ElementName=MyMap}" LayerIDs="MyGraphicsLayer" GeometryServiceUrl="http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer" />
        </Grid.Resources>
       
        <Canvas Width="545" Height="107" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,0,20,0" ></Canvas>
        <Border Background="#DD919191" BorderThickness="1" CornerRadius="5" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,1,0,0" BorderBrush="Black" Height="106">
            <Border.Effect>
                <DropShadowEffect />
            </Border.Effect>
            <Grid Margin="10" Background="White" Width="555">
                <StackPanel Orientation="Vertical" Height="101" Margin="0,0,14,-22">
                    <StackPanel x:Name="MyStackPanel" Orientation="Horizontal" Width="521">
                        <Button Tag="DrawPoint" Margin="5" Click="Tool_Click" ToolTipService.ToolTip="Add a point">
                            <Image Source="http://ssasgis/Assets/images/DrawPoint.png" Stretch="UniformToFill" Margin="2" />
                        </Button>
                        <Button Tag="DrawPolyline" Margin="5" Click="Tool_Click" ToolTipService.ToolTip="Add a polyline">
                            <Image Source="http://ssasgis/Assets/images/DrawPolyline.png" Stretch="UniformToFill" Margin="2" />
                        </Button>
                        <Button Tag="DrawPolygon" Margin="5" Click="Tool_Click" ToolTipService.ToolTip="Add a polygon">
                            <Image Source="http://ssasgis/Assets/images/DrawPolygon.png" Stretch="UniformToFill" Margin="2" />
                        </Button>
                       
                        <Button Tag="ClearStopDraw" Margin="5" Click="Tool_Click" Style="{x:Null}" ToolTipService.ToolTip="Clear graphics">
                            <Image Source="http://ssasgis/Assets/images/StopDraw.png" Stretch="UniformToFill" Margin="2" />
                        </Button>
                    </StackPanel>
                    <!--<Button Command="{Binding EditVertices}" Margin="2">
                        <TextBlock>Edit Selected Shape</TextBlock>
                    </Button>-->
                    <CheckBox x:Name="EnableEditVerticesScaleRotate" Content="Click on geometry to edit"
                          IsChecked="False" Foreground="Black" FontWeight="Bold" Margin="10" />
                </StackPanel>
            </Grid>
        </Border>
    </Grid>  

   
</UserControl>
0 Kudos
3 Replies
TomMagdaleno
Frequent Contributor
Hi Scott,
  Did you ever have any luck getting this working?  It looks like a great tool.
0 Kudos
MarcGraham
Deactivated User
Has anybody got a draw tool working with the Silverlight viewer?

That would be super awesome.
0 Kudos
Hwa_SaupLee
Occasional Contributor
Is there anyone make the graphic drawing tool working in the silverlight viewer?
0 Kudos