Select to view content in your preferred language

Question for TextSymbol ControlTemplate

2835
2
01-29-2013 05:07 AM
ahlzl
by
Emerging Contributor
WINXP SP3 + VS2010SP1 + Siverlight5 + API3.0


my XML code:
<UserControl xmlns:esri="http://schemas.esri.com/arcgis/client/2009"  x:Class="SL_TextSymbol.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"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.Resources>  
            <esri:SimpleMarkerSymbol x:Key="RedCircleSymbol" Color="Red" Size="12" Style="Circle" />
            <esri:TextSymbol x:Key="MyTextSymbol">
                <esri:TextSymbol.ControlTemplate>
                    <ControlTemplate>
                        <Border Background="White" BorderBrush="Black">
                            <TextBlock x:Name="MyInfo"
                                FontSize="{Binding Symbol.Size}" 
                                Text="{Binding Symbol.Text}"
                                Foreground="Blue"
                                FontFamily="{Binding Symbol.FontFamily}"
                                HorizontalAlignment="Center" />
                        </Border>
                    </ControlTemplate>
                </esri:TextSymbol.ControlTemplate>
            </esri:TextSymbol>
        </Grid.Resources>

        <esri:Map x:Name="MyMap" WrapAround="True" Background="White">
            <esri:ArcGISTiledMapServiceLayer ID="PhysicalTiledLayer" 
                Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer"/>
            <esri:GraphicsLayer ID="MyGraphicsLayer" />
        </esri:Map>
    </Grid>
</UserControl>




My C# code:

using System.Windows.Controls;
using System.Windows.Media;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;
using ESRI.ArcGIS.Client.Projection;
using ESRI.ArcGIS.Client.Symbols;

namespace SL_TextSymbol
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;

            // one
            Graphic graphic1 = new Graphic()
            {
                Geometry = new MapPoint(118, 35),
                Symbol = LayoutRoot.Resources["RedCircleSymbol"] as Symbol
            };
            graphicsLayer.Graphics.Add(graphic1);

            TextSymbol textSymbol1 = LayoutRoot.Resources["MyTextSymbol"] as TextSymbol;
            Graphic graphicText1 = new Graphic()
            {
                Geometry = new MapPoint(118, 35),
                Symbol = textSymbol1
            };

            textSymbol1.FontSize = 12;
            textSymbol1.Foreground = new SolidColorBrush(Colors.Blue);
            textSymbol1.Text = "one";
            textSymbol1.OffsetX = -10;
            graphicsLayer.Graphics.Add(graphicText1);


            // two
            Graphic graphic2 = new Graphic()
            {
                Geometry = new MapPoint(1800000, 50000),
                Symbol = LayoutRoot.Resources["RedCircleSymbol"] as Symbol
            };
            graphicsLayer.Graphics.Add(graphic2);

            TextSymbol textSymbol2 = LayoutRoot.Resources["MyTextSymbol"] as TextSymbol;
            Graphic graphicText2 = new Graphic()
            {
                Geometry = new MapPoint(1800000, 50000),
                Symbol = textSymbol2
            };

            textSymbol2.FontSize = 12;
            textSymbol2.Foreground = new SolidColorBrush(Colors.Blue);
            textSymbol2.Text = "two";
            textSymbol2.OffsetX = -10;
            graphicsLayer.Graphics.Add(graphicText2);  
        }
    }
}


[ATTACH=CONFIG]21160[/ATTACH]

I want "one" and "two". but "two" and "two".
thanks!
0 Kudos
2 Replies
DominiqueBroux
Esri Frequent Contributor
TextSymbol textSymbol1 = LayoutRoot.Resources["MyTextSymbol"] as TextSymbol;
.......
TextSymbol textSymbol2 = LayoutRoot.Resources["MyTextSymbol"] as TextSymbol;


Despite different names, both variables point to the same text symbol.
When you change the symbol, your change applies to all graphics using this symbol, i.e all your graphics.

You should either create different symbols (if there are few possible Text values) or, likely better, bind the symbol Text to a graphic attribute, so you don't have to change the symbol. i.e something like:


            <esri:TextSymbol x:Key="MyTextSymbol" Text={Binding Attributes[myFieldUsedForLabelling]}>
                <esri:TextSymbol.ControlTemplate>
                    <ControlTemplate>
                        <Border Background="White" BorderBrush="Black">
                            <TextBlock x:Name="MyInfo"
                                FontSize="{Binding Symbol.Size}" 
                                Text="{Binding Symbol.Text}"
                                Foreground="Blue"
                                FontFamily="{Binding Symbol.FontFamily}"
                                HorizontalAlignment="Center" />
                        </Border>
                    </ControlTemplate>
                </esri:TextSymbol.ControlTemplate>
            </esri:TextSymbol>


or directly in the template:

            <esri:TextSymbol x:Key="MyTextSymbol">
                <esri:TextSymbol.ControlTemplate>
                    <ControlTemplate>
                        <Border Background="White" BorderBrush="Black">
                            <TextBlock x:Name="MyInfo"
                                FontSize="{Binding Symbol.Size}" 
                                Text="{Binding Attributes[myFieldUsedForLabelling]}"
                                Foreground="Blue"
                                FontFamily="{Binding Symbol.FontFamily}"
                                HorizontalAlignment="Center" />
                        </Border>
                    </ControlTemplate>
                </esri:TextSymbol.ControlTemplate>
            </esri:TextSymbol>
0 Kudos
ahlzl
by
Emerging Contributor
Despite different names, both variables point to the same text symbol.
When you change the symbol, your change applies to all graphics using this symbol, i.e all your graphics.

You should either create different symbols (if there are few possible Text values) or, likely better, bind the symbol Text to a graphic attribute, so you don't have to change the symbol. i.e something like:


            <esri:TextSymbol x:Key="MyTextSymbol" Text={Binding Attributes[myFieldUsedForLabelling]}>
                <esri:TextSymbol.ControlTemplate>
                    <ControlTemplate>
                        <Border Background="White" BorderBrush="Black">
                            <TextBlock x:Name="MyInfo"
                                FontSize="{Binding Symbol.Size}" 
                                Text="{Binding Symbol.Text}"
                                Foreground="Blue"
                                FontFamily="{Binding Symbol.FontFamily}"
                                HorizontalAlignment="Center" />
                        </Border>
                    </ControlTemplate>
                </esri:TextSymbol.ControlTemplate>
            </esri:TextSymbol>


or directly in the template:

            <esri:TextSymbol x:Key="MyTextSymbol">
                <esri:TextSymbol.ControlTemplate>
                    <ControlTemplate>
                        <Border Background="White" BorderBrush="Black">
                            <TextBlock x:Name="MyInfo"
                                FontSize="{Binding Symbol.Size}" 
                                Text="{Binding Attributes[myFieldUsedForLabelling]}"
                                Foreground="Blue"
                                FontFamily="{Binding Symbol.FontFamily}"
                                HorizontalAlignment="Center" />
                        </Border>
                    </ControlTemplate>
                </esri:TextSymbol.ControlTemplate>
            </esri:TextSymbol>


succeed and thanks!
0 Kudos