Select to view content in your preferred language

Rotate text in Code

833
1
12-27-2010 11:12 AM
RobertMEIER
Regular Contributor
I found this post on the blog -

http://blogs.esri.com/Dev/blogs/silverlightwpf/archive/2010/05/04/Take-advantage-of-new-Silverlight-...

I am intrested in this section of the blog -
Binding to DependencyObject

Prior to Silverlight 4, you were only allowed to set binding expressions on objects that inherit from FrameworkElement. This limitation has been relaxed so now you can bind to anything that inherits from DependencyObject (like in WPF). This means you can bind an attribute to a symbol and rotate it based on the attribute value. Example:

<esri:MarkerSymbol x:Key="MySymbol">  
<esri:MarkerSymbol.ControlTemplate>    
<ControlTemplate>      
<TextBlock Text="=>">        
<TextBlock.RenderTransform>          
<RotateTransform Angle="{Binding Attributes[Heading]}" />         </TextBlock.RenderTransform>      
</TextBlock>    
</ControlTemplate>  
</esri:MarkerSymbol.ControlTemplate></esri:MarkerSymbol >


How do you so this in code - here is the code i am attemting to use.

            Dim ct As ControlTemplate = New ControlTemplate()

            Dim rt As RotateTransform = New RotateTransform()
            rt.Angle = 125
            Dim tbx As TextBlock = New TextBlock()
            tbx.Text = "this is text"
            tbx.RenderTransform = rt

            ct.SetValue(Nothing, tbx) -- this is the part I can't figure out.

            Dim marker As New MarkerSymbol()
            marker.ControlTemplate = ct

Any help is appreciated!

-bert
0 Kudos
1 Reply
RobertMEIER
Regular Contributor
Ok, i guess I had to ask for help to figure this out. I created a tool to place text on the map and wanted to set the angle.

I used Mortens example from the blog referenced in the previous post, but changed it slightly.

I added a binding element for the text value.

My solution, I created a symbol in the xaml like this  -
            <esri:MarkerSymbol x:Name="MyAngleTextSymbol">
                <esri:MarkerSymbol.ControlTemplate>
                    <ControlTemplate>
                        <TextBlock Text="{Binding Attributes[Content]}">       
                            <TextBlock.RenderTransform>          
                                <RotateTransform Angle="{Binding Attributes[Heading]}" />       
                            </TextBlock.RenderTransform>
                        </TextBlock>
                    </ControlTemplate>
                </esri:MarkerSymbol.ControlTemplate>
            </esri:MarkerSymbol >

Then in code when I create the graphic I also add the attributes to the graphic -

Private Sub GraphicDrawObject_DrawComplete(ByVal sender As Object, ByVal args As ESRI.ArcGIS.Client.DrawEventArgs)
        Dim graphicsLayer As GraphicsLayer = TryCast(MainMap.Layers("InteractiveGraphicsLayer"), GraphicsLayer)
        If TypeOf _activeSymbol Is TextSymbol Then

            Dim graphic As New ESRI.ArcGIS.Client.Graphic() With {.Geometry = args.Geometry, .Symbol = MyAngleTextSymbol}
            graphic.Attributes.Add("Content", tbxGraphicText.Text)
            graphic.Attributes.Add("Heading", tbxTextAngle.Text)
            graphicsLayer.Graphics.Add(graphic)

        Else

            Dim graphic As New ESRI.ArcGIS.Client.Graphic() With {.Geometry = args.Geometry, .Symbol = _activeSymbol}
            graphicsLayer.Graphics.Add(graphic)
        End If

    End Sub
0 Kudos