Select to view content in your preferred language

MapTip's GraphicLayer Binding

872
3
03-04-2011 12:43 PM
SteveLi
Emerging Contributor
I can't make MapTip's GraphicLayer binding to work:
in xaml:
<Canvas HorizontalAlignment="Left" VerticalAlignment="Top" >
            <esri:MapTip x:Name="MyMapTip"   BorderBrush="#99000000"  Title="{Binding [OBJECTID]}"
                GraphicsLayer="{Binding Path=mapTipLayer}"
                BorderThickness="1" VerticalOffset="10" Opacity="0.7"
                HorizontalOffset="10" Background="#DDFFFFFF" />
        </Canvas>



in my ViewModel, I defined a mapTipLayer member:
public FeatureLayer mapTipLayer
            {
                get { return (FeatureLayer)GetValue(mapTipLayerProperty); }
                set { SetValue(mapTipLayerProperty, value); }
            }
            public static readonly DependencyProperty mapTipLayerProperty =
                DependencyProperty.Register("mapTipLayer", typeof(FeatureLayer), typeof(MainPage), new PropertyMetadata(null));



and I am adding layers dynamically and set mapTipLayer:
foreach (MapLayer mapLayer in mapLayers) // Loop through List with foreach
                {
                    Debug.WriteLine(mapLayer.Url);
                    if (mapLayer.Type == "ArcGISTiledMapServiceLayer")
                    {
                        Layers.Add(new ArcGISTiledMapServiceLayer() { ID=mapLayer.ID, Url=mapLayer.Url, Visible=mapLayer.Visible, Opacity=mapLayer.Opacity });
                    }
                    else if (mapLayer.Type == "FeatureLayer")
                    {
                        FeatureLayer fLayer = new FeatureLayer() { ID = mapLayer.ID, Url = mapLayer.Url, Visible = mapLayer.Visible, Opacity = mapLayer.Opacity };
                        mapTipLayer = fLayer;
                        Layers.Add(fLayer);
                    }
                }

Can someone help please?  Thanks.
0 Kudos
3 Replies
SteveLi
Emerging Contributor
in my xaml, i also tried

GraphicsLayer="{Binding mapTipLayer}"

it didn't work either.
0 Kudos
JenniferNery
Esri Regular Contributor
If you modify this sample http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#MapTipWidget to set MapTip GraphicsLayer property in XAML with Element Binding:
GraphicsLayer="{Binding ElementName=MyMap, Path=Layers[MyGraphicsLayer]}"

and comment-out this line in code-behind under (QueryTask_ExecuteCompleted)
//MyMapTip.GraphicsLayer = graphicsLayer;
the sample still works.

In your sample, it is difficult to tell what is wrong with your binding. But I suggest you check that MapTip.DataContext is pointed to an instance of your Model.

For example this binding statement:
GraphicsLayer="{Binding SomeProperty}"
assumes that DataContext has been set to an object that contains a property SomeProperty of type GraphicsLayer. FeatureLayer is GraphicsLayer so it should have worked with your mapTipLayer property.

I suspect that DataContext is null or not the object that contains this property. Do you define a model class or are you using MainPage as your model?

DependencyProperty.Register("mapTipLayer", typeof(FeatureLayer), typeof(  MainPage), new PropertyMetadata(null));


I think that if you are following MVVM pattern, mapTipLayer should have been a property of your Model class, separate from the MainPage UserControl.
0 Kudos
dotMorten_esri
Esri Notable Contributor
Only FrameworkElements has a DataContext property (which a Layer is not), so you can't do binding without specific a source, either by using the source property in the binding or specifying the element name as Jennifer suggested.
0 Kudos