Select to view content in your preferred language

When FeatureLayer creating Graphics....

822
4
08-03-2010 03:33 AM
linusang
Emerging Contributor
I would like to attach it's FeatureLayer Id to each Graphic's Symbol's Path using Attach Properties when the FeatureLayer creates it's Layer.. how do i do that?
0 Kudos
4 Replies
AliMirzabeigi
Emerging Contributor
1) Define an attached property in your class:
private static readonly DependencyProperty AssociatedLayerIDProperty =
   DependencyProperty.RegisterAttached("AssociatedLayerID", typeof(string), typeof(Graphic), null);

2) Use the following code snippet in CollectionChanged event of Graphics or UpdateCompleted event of your FeatureLayer:
FeatureLayer featureLayer = sender as FeatureLayer;
   foreach (Graphic graphic in featureLayer)
   {
    graphic.SetValue(AssociatedLayerIDProperty, featureLayer.ID);
   }


* You can retrieve these value later using the following code snippet:
string associatedField = (sender as FrameworkElement).GetValue(AssociatedLayerIDProperty) as string;
0 Kudos
linusang
Emerging Contributor
thanks i manage to hook it up the attach property to the Graphics via the collectionchange... however in my controltemplate for the symbol... i goet the property not found binding error

<esri:LineSymbol x:Key="DefaultLineSymbol">
        <esri:LineSymbol.ControlTemplate>
            <ControlTemplate>
                <Grid>
                    <Path x:Name="Element" Stroke="{Binding Symbol.Color}" StrokeThickness="{Binding Symbol.Width}" Tag="{Binding Path=(my:MyClass.LayerId)}"/>                    
                    </Path>
                </Grid>                
            </ControlTemplate>
        </esri:LineSymbol.ControlTemplate>
    </esri:LineSymbol>
0 Kudos
AliMirzabeigi
Emerging Contributor
Well... why aren't you just using the LayerID as the tag of your symbology instead? You can use ElementBinding for your Tag as the following:
{Binding Layers[1].ID, ElementName=MyMap}

Where MyMap is your map control.
0 Kudos
linusang
Emerging Contributor
Sorry but my scenario is actually more than just applying the symbol resource... the user is able to add and remove layers dynamically... change the symbology dynamically as well

this is the actual code.. i stripped out the irrelevant details in the previous post.... i'm doing a multi-touch app and this path symbol resource will actually show a custom maptip via an Action.... however i need the LayerId info to be passed into the Action class as well.. the Attributes properties are able to pass the details but the attached property reports as null... not sure why... is it because the CollectionChanged event happened after rendering the resource? that is why it is not able to retrieve the attach property?

    <esri:LineSymbol x:Key="DefaultLineSymbol" x:Shared="False">
        <esri:LineSymbol.ControlTemplate>
            <ControlTemplate>
                <Grid>
                    <Path x:Name="Element" Stroke="{Binding Symbol.Color}" StrokeThickness="{Binding Symbol.Width}"/>
                    <Path x:Name="TouchArea" Stroke="Transparent" StrokeThickness="10" Data="{Binding Data, ElementName=Element}" >
                        <i:Interaction.Triggers>
                            <gti:TouchTrigger HandleTouches="True" >
                                <gti:PathMapTipAction Attributes="{Binding Attributes}" LayerId={Binding Path=(my:MyClass.LayerId)} />
                            </gti:TouchTrigger>
                        </i:Interaction.Triggers>
                    </Path>
                </Grid>                
        </esri:LineSymbol.ControlTemplate>
    </esri:LineSymbol>


EDIT: i got this error for the above code

System.Windows.Data Error: 17 : Cannot get 'LayerId' value (type 'String') from '' (type 'DataBinding'). BindingExpression:Path=(0); DataItem='DataBinding' (HashCode=18192389); target element is 'PathMapTipAction' (HashCode=46874050); target property is 'LayerId' (type 'String') InvalidCastException:'System.InvalidCastException: Unable to cast object of type 'ESRI.ArcGIS.Client.DataBinding' to type 'System.Windows.DependencyObject'.
0 Kudos