Select to view content in your preferred language

How to reference the controls in a FeatureLayer?

780
2
04-06-2011 12:51 PM
weiliang
Deactivated User
Hi there,

First question: I have controls enclosed in the maptip of one FeatureLayer in xaml, and I want to reference these controls (such as HyperlinkButton) in the code by their Name. However, these control is null in the C# code (e.g., this.hyperlinkButton.NavigateUri , and hyperlinkButton is null with a runtime error). Is there certain way to reference these controls enclosed by the maptip?

Second question, it seems that the FeatureLayer's associated URL can't be changed in code once initialized. Is there any way to work around this limitation?

Thanks for your help!

Wei
0 Kudos
2 Replies
JenniferNery
Esri Regular Contributor
For the first question:
The control inside your map tip is null because it only part of the template.

Let's try the following SDK sample: http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#FeatureLayerMapTips
FeatureLayer.MapTip was defined in XAML (I simply added the code in red):
<Border CornerRadius="10" BorderBrush="#FF222957" BorderThickness="3" Margin="0,0,15,15">
    <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>
    <Border.Effect>
        <DropShadowEffect ShadowDepth="10" BlurRadius="14" Direction="300" />
    </Border.Effect>
    <StackPanel Margin="7">
        <TextBlock x:Name="MyCityName"  Loaded="TextBlock_Loaded"  Text="{Binding [CITY_NAME]}" FontWeight="Bold" Foreground="Black"  />
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="Population: " Foreground="Black" />
            <TextBlock Text="{Binding [POP1990]}" Foreground="Black" />
        </StackPanel>
    </StackPanel>
</Border>


I can think of two ways you can access the in code. Since you know the template, you can cast the elements until you get to the control of interest or simply add a Loaded event on the control.
{
 InitializeComponent();
 FeatureLayer layer = this.MyMap.Layers["CitiesFeatureLayer"] as FeatureLayer;
 var mapTip = layer.MapTip;
 if (this.MyCityName == null)
 {
  var name = (((mapTip as Border).Child as StackPanel).Children[0] as TextBlock).Name;
  System.Diagnostics.Debug.WriteLine("This is one way of accessing this control '{0}'", name);
 }
}

private void TextBlock_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
 TextBlock tb = sender as TextBlock;
 if (this.MyCityName == null)
  System.Diagnostics.Debug.WriteLine("Another way of accessing this control '{0}'", tb.Name);
}


For the second question:
This is by design. If URL should change, you need to replace the FeatureLayer with a new instance.
0 Kudos
weiliang
Deactivated User
Many thanks for your kindly help, Jennifer. You guys are really helpful and knowledgeable!

Appreciate it very very much.

Wei
0 Kudos