MapTip textblock binding in C# code behind not in .xaml

3003
6
10-21-2012 11:45 PM
MatthewWeed
New Contributor II
Hi,

I have used binding of textblock in xaml. But as per my requirement featurelayer will not be shown by default so when user check any chexkbox to show this featurelayer.
Below I  have shown two pieces of code 1 is design time and other is at runtime.
when I am implementing this on designtime maptip is coming but not @ runtime

1 Design time:  Here Map Tip working fine
<esri:FeatureLayer ID="MyFeatureLayer"
                    Url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/Map..."
                    Where="POP1990 > 75000" FeatureSymbol="{StaticResource MediumMarkerSymbol}" >
                    <esri:FeatureLayer.Clusterer>
                        <samples:SumClusterer AggregateColumn="POP1990" SymbolScale="0.001" />
                    </esri:FeatureLayer.Clusterer>
                    <esri:FeatureLayer.OutFields>
                        <sys:String>CITY_NAME</sys:String>
                        <sys:String>POP1990</sys:String>
                    </esri:FeatureLayer.OutFields>
                    <esri:FeatureLayer.MapTip>
                        <Grid Background="LightYellow">
                            <StackPanel Margin="5">
                                <TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter},
                                    ConverterParameter=CITY_NAME, Mode=OneWay}" FontWeight="Bold" />
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="Population (1990): " />
                                    <TextBlock Text="{Binding Converter={StaticResource MyDictionaryConverter},
                                        ConverterParameter=POP1990, Mode=OneWay}" />
                                </StackPanel>
                            </StackPanel>
                            <Border BorderBrush="Black" BorderThickness="1" />
                        </Grid>
                    </esri:FeatureLayer.MapTip>
                </esri:FeatureLayer>



2.Runtime : Here Map tips are not coming.
FeatureLayer featureLayer = new FeatureLayer ();
featureLayer.ID = "MyFeatureLayer";
featureLayer.Url = ="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/Map..." ;
featureLayer.OutFields.AddRange(new string[] { "CITY_NAME", "POP1990" });
FlareClusterer flclusterer = new FlareClusterer();
Grid grid = new Grid();
StackPanel objStackPanel = new StackPanel();
objStackPanel.Orientation = Orientation.Horizontal;
TextBlock textBlock = new TextBlock();
System.Windows.Data.Binding binding = new System.Windows.Data.Binding("CITY_NAME");
binding.Source = featureLayer.OutFields;
textBlock.SetBinding(TextBlock.TextProperty, binding); // run fine but problem in binding as it does not shows any tooltip
objStackPanel.Children.Add(textBlock);
SolidColorBrush objColor = new SolidColorBrush(Colors.Yellow);
flclusterer.FlareBackground = objColor;
flclusterer.FlareForeground = new SolidColorBrush(Colors.Blue);
flclusterer.MaximumFlareCount = 9;
flclusterer.Radius = 9;
grid.Children.Add(objStackPanel);
featureLayer.Clusterer = flclusterer;
featureLayer.MapTip = grid;

Thanks In advance.
0 Kudos
6 Replies
SanajyJadhav
Occasional Contributor II
This is how I implemented maptips @runtime.

1. I subscribed to the mouse enter event of the feature layer and used following code.
   Graphic g = e.Graphic;

                MapTipsPanel mt = new MapTipsPanel();
                mt.lblFieldName.Text = fn; //this is field name coming from some where else
                mt.txtLyrName.Text = lyeName; //this is layer name coming from some where else

                string maptipsValue= Convert.ToString(g.Attributes[fn]);
                mt.txtFieldValue.Text = maptipsValue + " " + suffixVal;

                fl.MapTip = mt;



Note that MapTipsPanel  in above code is a separate user control and its xaml is as below.

XAML:
 <Grid x:Name="LayoutRoot" Width="auto" Height="70">
        <Border CornerRadius="10" BorderBrush="#FF222957" BorderThickness="3" >
            <Border.Background>
                <LinearGradientBrush EndPoint="1.038,1.136" StartPoint="0.015,0.188">
                    <GradientStop Color="#FFD1DFF2"/>
                    <GradientStop Color="#FF0088FF" Offset="0.946"/>
                </LinearGradientBrush>
            </Border.Background>

            <StackPanel Orientation="Vertical">
                <TextBlock x:Name="txtLyrName" Height="20" Width="auto" Margin="5,5,5,0"  Text="Layer Name"/>
                <TextBlock x:Name="lblFieldName" Height="20" Width="auto" Margin="5,0,5,0" Text=" Field name" />
                <TextBlock x:Name="txtFieldValue" Height="20" Width="auto" Margin="5,0,5,0" Text=" Attr value" />
            </StackPanel>
        </Border>
    </Grid>


This worked for me. Hope this helps.
0 Kudos
DominiqueBroux
Esri Frequent Contributor
when I am implementing this on designtime maptip is coming but not @ runtime


Your XAML implementation uses a DictionaryConverter but you forgot it in C#.

That being said, from Silverlight 4, the DictionaryConverter is no more useful. You may use the '[]' binding syntax, i.e.:

System.Windows.Data.Binding binding = new System.Windows.Data.Binding("[CITY_NAME]");
0 Kudos
MatthewWeed
New Contributor II
This is how I implemented maptips @runtime.

1. I subscribed to the mouse enter event of the feature layer and used following code.
   Graphic g = e.Graphic;

                MapTipsPanel mt = new MapTipsPanel();
                mt.lblFieldName.Text = fn; //this is field name coming from some where else
                mt.txtLyrName.Text = lyeName; //this is layer name coming from some where else

                string maptipsValue= Convert.ToString(g.Attributes[fn]);
                mt.txtFieldValue.Text = maptipsValue + " " + suffixVal;

                fl.MapTip = mt;



Note that MapTipsPanel  in above code is a separate user control and its xaml is as below.

XAML:
 <Grid x:Name="LayoutRoot" Width="auto" Height="70">
        <Border CornerRadius="10" BorderBrush="#FF222957" BorderThickness="3" >
            <Border.Background>
                <LinearGradientBrush EndPoint="1.038,1.136" StartPoint="0.015,0.188">
                    <GradientStop Color="#FFD1DFF2"/>
                    <GradientStop Color="#FF0088FF" Offset="0.946"/>
                </LinearGradientBrush>
            </Border.Background>

            <StackPanel Orientation="Vertical">
                <TextBlock x:Name="txtLyrName" Height="20" Width="auto" Margin="5,5,5,0"  Text="Layer Name"/>
                <TextBlock x:Name="lblFieldName" Height="20" Width="auto" Margin="5,0,5,0" Text=" Field name" />
                <TextBlock x:Name="txtFieldValue" Height="20" Width="auto" Margin="5,0,5,0" Text=" Attr value" />
            </StackPanel>
        </Border>
    </Grid>


This worked for me. Hope this helps.



Thanks Sanjay,

It works for me.
But i am using clustering with the help of flayering. For simple map points it worked for me, but when i tried to get maptip in case of clustering, it doesn't works.

Any suggestion to handle textblock in clustering case.
0 Kudos
SanajyJadhav
Occasional Contributor II
Well, in my opinion, it should not work for the clusterer. Because, clusterer means you are hiding some point features and displaying few points to represent them. So, when mouse enter event is fired, it would not know which graphic is below the mouse.

That's what I think.

@Dominique,

Actually I had tried your approach earlier and it worked when I put literal field name in [ ]. But when I put the string variable representing the field name in [ ], it did not work. That's why I took another approach. Can you please explain this behaviour?
0 Kudos
MatthewWeed
New Contributor II
Well, in my opinion, it should not work for the clusterer. Because, clusterer means you are hiding some point features and displaying few points to represent them. So, when mouse enter event is fired, it would not know which graphic is below the mouse.

That's what I think.

@Dominique,

Actually I had tried your approach earlier and it worked when I put literal field name in [ ]. But when I put the string variable representing the field name in [ ], it did not work. That's why I took another approach. Can you please explain this behaviour?


@Sanjay I got the point You said..
But any solution in this regard for clustering because when i tried to implement binding in xaml it worked fine.

But why not in xaml.cs?
0 Kudos
MatthewWeed
New Contributor II
Can any one have idea regarding implementing MAPTIP through template?
0 Kudos