DrawGraphics Tools...can text be added?

1334
11
Jump to solution
07-30-2012 09:29 AM
ChristineZeller
New Contributor III
Is there a way to write Text (similar to Flex draw graphic tools)

http://resources.arcgis.com/en/help/silverlight-api/samples/start.htm#DrawGraphics

Thanks
Christine
0 Kudos
1 Solution

Accepted Solutions
TerryGiles
Occasional Contributor III
Christine,

In your switch statement, it looks like _activeSymbol is getting set to a MarkerSymbol for the 'EnterText' case.  Try making a TextSymbol instead, example below.

                    activeSymbol = new TextSymbol()                     {                         Text = "",                         FontSize = 12,                         Foreground = new SolidColorBrush(clr),                         FontFamily = new FontFamily("Arial"),                     }; 

View solution in original post

0 Kudos
11 Replies
TerryGiles
Occasional Contributor III
What I've done is set the DrawMode to Point and then put the text the user types in a textbox at that point - code snippet below.  Might work for other DrawMode geometry types too, just haven't tried it. 

void Draw_DrawComplete(object sender, DrawEventArgs e)
        {
            GraphicsLayer gl = _Map.Layers[_glayer] as GraphicsLayer;

            if (_bolText)  //if user selected text from drawing options
            {
                (_symbol as TextSymbol).Text = txtText.Text;
                txtText.Text = string.Empty;
                spText.Visibility = Visibility.Collapsed;
            }

            Graphic g = new Graphic() 
            {
                Geometry = e.Geometry,
                Symbol = _symbol
            };

            gl.Graphics.Add(g);

            gl.Refresh();

            _Draw.IsEnabled = false;
            _Draw.DrawMode = DrawMode.None;
        }
0 Kudos
nakulmanocha
Esri Regular Contributor
You can add the TextSymbol as Graphic to the Map. Here is a sample

http://resources.arcgis.com/en/help/silverlight-api/samples/start.htm#AddGraphicsXAML



               <esri:Graphic>
                        <esri:Graphic.Symbol>
                            <esri:TextSymbol FontFamily="Arial" FontSize="14" Foreground="Black" Text="My Text" />
                        </esri:Graphic.Symbol>
                        <esri:MapPoint X="-10.609" Y="23.729">
                            <esri:Geometry.SpatialReference >
                                <esri:SpatialReference WKID="4326" />
                            </esri:Geometry.SpatialReference>
                        </esri:MapPoint>
                    </esri:Graphic>


Another way to use a ElementLayer and add a textBox. Here is a sample using ElementLayer
http://resources.arcgis.com/en/help/silverlight-api/samples/start.htm#ElementLayer
0 Kudos
ChristineZeller
New Contributor III
What I've done is set the DrawMode to Point and then put the text the user types in a textbox at that point - code snippet below.  Might work for other DrawMode geometry types too, just haven't tried it. 

void Draw_DrawComplete(object sender, DrawEventArgs e)
        {
            GraphicsLayer gl = _Map.Layers[_glayer] as GraphicsLayer;

            if (_bolText)  //if user selected text from drawing options
            {
                (_symbol as TextSymbol).Text = txtText.Text;
                txtText.Text = string.Empty;
                spText.Visibility = Visibility.Collapsed;
            }

            Graphic g = new Graphic() 
            {
                Geometry = e.Geometry,
                Symbol = _symbol
            };

            gl.Graphics.Add(g);

            gl.Refresh();

            _Draw.IsEnabled = false;
            _Draw.DrawMode = DrawMode.None;
        }





Terry thanks for the post.  I step away from this awhile but I'm back and I'm having trouble figure out what is

if (_bolText)  //if user selected text from drawing options

Base on your comment it is checking to see if the user selected the text draw button but I'm not sure how and where to set and define _bolText?

Also what is spText?
spText.Visibility = Visibility.Collapsed;


Thanks Christine
0 Kudos
ChristineZeller
New Contributor III
Terry, Sorry for all the post I think I have the bool but still running into a empty error so I'm wondering if you are binding the text somehow. I figured I would paste my code and maybe you would have time to take a look.

It is failing at the line (_activeSymbol as TextSymbol).Text = txtText.Text; and stating object null but i have text in my textbox so I'm wonding what I"m missing.
XAML

  <StackPanel Orientation="Vertical" Margin="3">
                        <StackPanel x:Name="MyStackPanel" Orientation="Horizontal">
                            <Button Tag="DrawPoint" Margin="5" Click="Tool_Click" 
                                ToolTipService.ToolTip="Add a point">
                                <Image Source="images/DrawPoint.png" Margin="2" />
                            </Button>
                            <Button Tag="DrawPolyline" Margin="5" Click="Tool_Click"
                                ToolTipService.ToolTip="Add a polyline">
                                <Image Source="images/DrawPolyline.png" Margin="2" />
                            </Button>
                            <Button Tag="DrawPolygon" Margin="5" Click="Tool_Click"
                                ToolTipService.ToolTip="Add a polygon">
                                <Image Source="images/DrawPolygon.png" Margin="2" />
                            </Button>
                            <Button Tag="DrawRectangle" Margin="5" Click="Tool_Click"
                                ToolTipService.ToolTip="Add a rectangle">
                                <Image Source="images/DrawRectangle.png" Margin="2" />
                            </Button>
                            <Button Tag="DrawFreehand" Margin="5" Click="Tool_Click"
                                ToolTipService.ToolTip="Add a freehand line">
                                <Image Source="images/DrawFreehand.png" Margin="2" />
                            </Button>
                            <Button Tag="DrawArrow" Margin="5" Click="Tool_Click"
                                ToolTipService.ToolTip="Add an arrow">
                                <Image Source="images/DrawArrow2.png" Margin="2" />
                            </Button>
                            <Button Tag="DrawTriangle" Margin="5" Click="Tool_Click"
                                ToolTipService.ToolTip="Add a triangle">
                                <Image Source="images/DrawTriangle2.png" Margin="2" />
                            </Button>
                            <Button Tag="DrawCircle" Margin="5" Click="Tool_Click"
                                ToolTipService.ToolTip="Add a circle">
                                <Image Source="images/DrawCircle2.png" Margin="2" />
                            </Button>
                            <Button Tag="DrawEllipse" Margin="5" Click="Tool_Click"
                                ToolTipService.ToolTip="Add an ellipse">
                                <Image Source="images/DrawEllipse2.png" Margin="2" />
                            </Button>
                            <Button Tag="EnterText" Margin="5" Click="Tool_Click"
                                ToolTipService.ToolTip="Add text">
                                <Image Source="images/text.png" Margin="2" />
                            </Button>
                            <Button Tag="DrawNothing" Margin="5" Click="Tool_Click"
                                 Style="{x:Null}"
                                ToolTipService.ToolTip="Unselect Current Tool">
                                <Image Source="images/dismiss.png" Margin="2" />
                            </Button>
                            <Button Tag="ClearStopDraw" Margin="5" Click="Tool_Click"
                                Style="{x:Null}"
                                ToolTipService.ToolTip="Clear graphics">
                                <Image Source="images/StopDraw.png" Margin="2" />
                            </Button>
                        </StackPanel>

                        <CheckBox x:Name="EnableEditVerticesScaleRotate" Content="Click on geometry to edit" 
                          IsChecked="False" Foreground="Black" FontWeight="Bold" Margin="10,5,5,5" />

                        <TextBox x:Name="txtText" Text="" />
                        
                    </StackPanel>





CODE BEHIND



 private void MyDrawObjectGraphic_DrawComplete(object sender, ESRI.ArcGIS.Client.DrawEventArgs args)
        {

            if (_bolText)  //if user selected text from drawing options
            {
                (_activeSymbol as TextSymbol).Text = txtText.Text;  //FAILING HERE STATING NULL
                txtText.Text = string.Empty;
               // spText.Visibility = Visibility.Collapsed;
            }

            ESRI.ArcGIS.Client.Graphic graphic = new ESRI.ArcGIS.Client.Graphic()
            {
                Geometry = args.Geometry,
                Symbol = _activeSymbol,
            };
            graphicsLayer.Graphics.Add(graphic);


            graphicsLayer.Refresh();

            
        }

        private void GraphicsLayerG_MouseLeftButtonUp(object sender, GraphicMouseButtonEventArgs e)
        {
            if (EnableEditVerticesScaleRotate.IsChecked.Value)
            {
                MyDrawObjectGraphic.DrawMode = DrawMode.None;
                UnSelectTools();
                Editor editor = LayoutRoot.Resources["MyGraphicEditor"] 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":
                    MyDrawObjectGraphic.DrawMode = DrawMode.Point;
                    _activeSymbol = LayoutRoot.Resources["DefaultGraphicMarkerSymbol"] as Symbol;
                    break;
                case "DrawPolyline":
                    MyDrawObjectGraphic.DrawMode = DrawMode.Polyline;
                    _activeSymbol = LayoutRoot.Resources["DefaultGraphicLineSymbol"] as Symbol;
                    break;
                case "DrawPolygon":
                    MyDrawObjectGraphic.DrawMode = DrawMode.Polygon;
                    _activeSymbol = LayoutRoot.Resources["DefaultGraphicFillSymbol"] as Symbol;
                    break;
                case "DrawRectangle":
                    MyDrawObjectGraphic.DrawMode = DrawMode.Rectangle;
                    _activeSymbol = LayoutRoot.Resources["DefaultGraphicFillSymbol"] as Symbol;
                    break;
                case "DrawFreehand":
                    MyDrawObjectGraphic.DrawMode = DrawMode.Freehand;
                    _activeSymbol = LayoutRoot.Resources["DefaultGraphicLineSymbol"] as Symbol;
                    break;
                case "DrawArrow":
                    MyDrawObjectGraphic.DrawMode = DrawMode.Arrow;
                    _activeSymbol = LayoutRoot.Resources["DefaultGraphicFillSymbol"] as Symbol;
                    break;
                case "DrawTriangle":
                    MyDrawObjectGraphic.DrawMode = DrawMode.Triangle;
                    _activeSymbol = LayoutRoot.Resources["DefaultGraphicFillSymbol"] as Symbol;
                    break;
                case "DrawCircle":
                    MyDrawObjectGraphic.DrawMode = DrawMode.Circle;
                    _activeSymbol = LayoutRoot.Resources["DefaultGraphicFillSymbol"] as Symbol;
                    break;
                case "DrawEllipse":
                    MyDrawObjectGraphic.DrawMode = DrawMode.Ellipse;
                    _activeSymbol = LayoutRoot.Resources["DefaultGraphicFillSymbol"] as Symbol;
                    break;
                case "EnterText":
                     MyDrawObjectGraphic.DrawMode = DrawMode.Point;
                    _activeSymbol = LayoutRoot.Resources["DefaultGraphicMarkerSymbol"] as Symbol;
                    _bolText = true;
                    break;
                case "DrawNothing":
                    MyDrawObjectGraphic.DrawMode = DrawMode.None;
                    break;
                default:
                    MyDrawObjectGraphic.DrawMode = DrawMode.None;
                    graphicsLayer.ClearGraphics();
                    break;
            }
            MyDrawObjectGraphic.IsEnabled = (MyDrawObjectGraphic.DrawMode != DrawMode.None);
        }


0 Kudos
TerryGiles
Occasional Contributor III
Hi Christine,

Before you get into the Switch statement in the Tool_Click routine, reset _bolText to false, otherwise, if the user draws some text and then tries to draw a point/line/ploy afterwards _bolText will still be true it will pass your if _bolText test.

Hope that helps,  Terry
0 Kudos
ChristineZeller
New Contributor III
Hi Christine,

Before you get into the Switch statement in the Tool_Click routine, reset _bolText to false, otherwise, if the user draws some text and then tries to draw a point/line/ploy afterwards _bolText will still be true it will pass your if _bolText test.

Hope that helps,  Terry


I type text in my text box, click on the text button - it goes through the Tool_Click, goes through my switch and finds the
case "EnterText":
                     MyDrawObjectGraphic.DrawMode = DrawMode.Point;
                    _activeSymbol = LayoutRoot.Resources["DefaultGraphicMarkerSymbol"] as Symbol;
                    _bolText = true;
                    break;


Then It allows me to click where I want to place the text and it fails on MyDrawObjectGraphic_DrawComplete at at the line  (_activeSymbol as TextSymbol).Text = txtText.Text; and states it null

 private void MyDrawObjectGraphic_DrawComplete(object sender, ESRI.ArcGIS.Client.DrawEventArgs args)
        {

            if (_bolText)  //if user selected text from drawing options
            {
                (_activeSymbol as TextSymbol).Text = txtText.Text;
                txtText.Text = string.Empty;
               // spText.Visibility = Visibility.Collapsed;
            }

            ESRI.ArcGIS.Client.Graphic graphic = new ESRI.ArcGIS.Client.Graphic()
            {
                Geometry = args.Geometry,
                Symbol = _activeSymbol,
            };
            graphicsLayer.Graphics.Add(graphic);


            graphicsLayer.Refresh();

            
        }


Hey Terry I added the reset for _bolText before the switch but I'm still running into the same error, I've attached a screen shot in debug mode...any other ideas?
0 Kudos
TerryGiles
Occasional Contributor III
Christine,

In your switch statement, it looks like _activeSymbol is getting set to a MarkerSymbol for the 'EnterText' case.  Try making a TextSymbol instead, example below.

                    activeSymbol = new TextSymbol()                     {                         Text = "",                         FontSize = 12,                         Foreground = new SolidColorBrush(clr),                         FontFamily = new FontFamily("Arial"),                     }; 
0 Kudos
ChristineZeller
New Contributor III
Terry,

Thanks that worked!  I was hoping to maybe ask you one more thing?


If I add text (ex. Test) and then add text again (ex. 2nd Text) the first text (Test) gets changed to the second text (2nd Text) upon adding the 2nd text item.  Is there a way to stop this and preserve the first text item?

Thanks a bunch
Christine.
0 Kudos
TerryGiles
Occasional Contributor III
Christine,

Can you post your revised code for the DrawComplete & button click methods?
0 Kudos