Select to view content in your preferred language

scalebar can't be loaded until make some action to the map

1008
8
11-02-2010 02:10 PM
DanDong
Deactivated User
Hi all,

I run into a problem of the scalebar. It can't be loaded until I zoomin or zoomout or full extent the map (using the navigation tool). I can't figure out why this happen. I add the navigation and toolbar to the map control in XMAL

<esri:Navigation HorizontalAlignment="Left"  VerticalAlignment="Bottom"
                     Map="{Binding ElementName=MyMap}" Grid.Column="1" Grid.Row="2" Name="MyNavigationTool" PanFactor="0.5" TabNavigation="Once" Style="{StaticResource NavigationStyle1}"/>

<esri:ScaleBar  x:Name="MyScaleBar" Grid.Column="1" HorizontalAlignment="Right"  Grid.Row="2" VerticalAlignment="Bottom" Style="{StaticResource ScaleBarStyle1}"
Map="{Binding ElementName=MyMap}" DisplayUnit="Miles" MapUnit="Feet" Foreground="Black" />

when running the project, the navigation appears, but the map and scalebar both can't be loaded, unless I use the navigation to make some action, the map and scalebar can appear.

Did anybody run into the same situation before? Or any suggestions on this? Thank you 🙂
0 Kudos
8 Replies
AliMirzabeigi
Emerging Contributor
I used your XAML code without the associated styles for the Navigation and ScaleBar controls and was not able to reproduce your problem. Looks like you have some issues in your styles.
0 Kudos
DanDong
Deactivated User
I used your XAML code without the associated styles for the Navigation and ScaleBar controls and was not able to reproduce your problem. Looks like you have some issues in your styles.


Thanks Ali. I paste my style below:

<Style x:Key="ScaleBarStyle1" TargetType="esri:ScaleBar">
   <Setter Property="FillColor1" Value="Yellow"/>
   <Setter Property="FillColor2" Value="Black"/>  
   <Setter Property="TargetWidth" Value="170.0"/>
   <Setter Property="FontSize" Value="10.0"/>
   <Setter Property="BarHeight" Value="20.0"/>
   <Setter Property="Foreground" Value="Black"/>
   <Setter Property="Template">
    <Setter.Value>
     <ControlTemplate TargetType="esri:ScaleBar">     
      <StackPanel Orientation="Horizontal" >
                            <Grid x:Name="ScaleBarBlock" Height="{TemplateBinding BarHeight}"  Width="{TemplateBinding TargetWidth}" VerticalAlignment="Center">
        <Grid.ColumnDefinitions>
         <ColumnDefinition Width="0.2*"/>
         <ColumnDefinition Width="0.2*"/>
         <ColumnDefinition Width="0.2*"/>
         <ColumnDefinition Width="0.4*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
                                    <RowDefinition Height="0.6*"/>
                                    <RowDefinition Height="0.4*"/>
                                </Grid.RowDefinitions>
                                <TextBlock Grid.Column="0" Grid.Row="0" Text="0" FontSize="{TemplateBinding FontSize}" Foreground="{TemplateBinding Foreground}"/>
        <TextBlock Grid.Column="1" Grid.Row="0" x:Name="ScaleBarValue1" Loaded="ScaleBarValue1_Loaded" FontSize="{TemplateBinding FontSize}" Foreground="{TemplateBinding Foreground}"/>                            
                                <TextBlock Grid.Column="3" Grid.Row="0" x:Name="ScaleBarValue2" Loaded="ScaleBarValue2_Loaded" FontSize="{TemplateBinding FontSize}" Foreground="{TemplateBinding Foreground}"/>
                                <Rectangle Grid.Column="0" Fill="{TemplateBinding FillColor2}" Grid.Row="1"/>
        <Rectangle Grid.Column="1" Fill="{TemplateBinding FillColor1}" Grid.Row="1"/>
        <Rectangle Grid.Column="2" Fill="{TemplateBinding FillColor2}" Grid.Row="1"/>
        <Rectangle Grid.Column="3" Fill="{TemplateBinding FillColor1}" Grid.Row="1"/>     
       </Grid>
                            <TextBlock x:Name="ScaleBarValue" Loaded="ScaleBarValue_Loaded" Foreground="{TemplateBinding Foreground}" VerticalAlignment="Top"/>
      </StackPanel>
     </ControlTemplate>
    </Setter.Value>
   </Setter>
  </Style>

ScaleBarValue1,2 and 3 are the textblocks used to display partial values of the scalevalue, like from the beginning of the scalevar it displays 0, then 20, then 60.
ScaleBarValue is the end value of the scalebar, like 100.

Could you please help me check where is the problem? Thank you 🙂
0 Kudos
AliMirzabeigi
Emerging Contributor
What do "ScaleBarValue1_Loaded", "ScaleBarValue2_Loaded", and "ScaleBarValue_Loaded" do in your style and why you have them?
0 Kudos
DanDong
Deactivated User
What do "ScaleBarValue1_Loaded", "ScaleBarValue2_Loaded", and "ScaleBarValue_Loaded" do in your style and why you have them?



I use the codes from this thread: http://forums.arcgis.com/threads/15157-Access-ScaleBars-Current-Value   , in #4, I write the codes according to this, i think the ScaleBarValue_Loaded is used to load the text of the textblock. Are they necessary?

Code-behind:

        TextBlock scaleBarValue;
        TextBlock scaleBarValue1;
        TextBlock scaleBarValue2;

        private void MyMap_ExtentChanged(object sender, ExtentEventArgs e)
        {
            double doubleValue=1;

            if (MyScaleBar.DisplayUnit == ScaleBarUnit.Miles)
            {
                doubleValue = Convert.ToDouble(scaleBarValue.Text.TrimEnd(" Miles".ToCharArray()));
            }
            else if (MyScaleBar.DisplayUnit == ScaleBarUnit.Feet)
            {
                doubleValue = Convert.ToDouble(scaleBarValue.Text.TrimEnd(" Feet".ToCharArray()));
            }

            double dValue1 = doubleValue * 2 / 10;
            double dValue2 = doubleValue * 6 / 10;

            string stringValue1 = Convert.ToString(Math.Round(dValue1));
            string stringValue2 = Convert.ToString(Math.Round(dValue2));

            scaleBarValue1.Text = stringValue1;
            scaleBarValue2.Text = stringValue2;


            //if the end scalebar value is smaller than 10 miles, turn the unit to feet
            if (scaleBarValue.Text != null)
            {
                if (MyScaleBar.DisplayUnit == ScaleBarUnit.Miles && Convert.ToDouble(scaleBarValue.Text.TrimEnd(" Miles".ToCharArray())) < 10)
                {
                    MyScaleBar.DisplayUnit = ScaleBarUnit.Feet;
                }
                else if (MyScaleBar.DisplayUnit == ScaleBarUnit.Feet && Convert.ToDouble(scaleBarValue.Text.TrimEnd(" Feet".ToCharArray())) > 52800)
                {
                    MyScaleBar.DisplayUnit = ScaleBarUnit.Miles;
                }
            }
}

        private void ScaleBarValue_Loaded(object sender, RoutedEventArgs e)
        {
            scaleBarValue = sender as TextBlock;           
        }

        private void ScaleBarValue1_Loaded(object sender, RoutedEventArgs e)
        {
            scaleBarValue1 = sender as TextBlock;
        }

        private void ScaleBarValue2_Loaded(object sender, RoutedEventArgs e)
        {
            scaleBarValue2 = sender as TextBlock;
        }
0 Kudos
JenniferNery
Esri Regular Contributor
The Loaded event on ScaleBarValue TextBlock is not necessary.  The thread you are referring to had a specific use case where the current value of ScaleBar is needed to be tracked and converted to a different unit. Ryan's follow-up post #5 proposed an easier way to retrieve the TextBlock using FindName().

If your application does not need to do this extra stuff and you only want to change the look and feel of the controls, just start from the default template. If you have Expression Blend, here's a blog on how it can be accomplished: http://blogs.esri.com/Dev/blogs/silverlightwpf/archive/2010/05/20/Use-control-templates-to-customize...
0 Kudos
DanDong
Deactivated User
The Loaded event on ScaleBarValue TextBlock is not necessary.  The thread you are referring to had a specific use case where the current value of ScaleBar is needed to be tracked and converted to a different unit. Ryan's follow-up post #5 proposed an easier way to retrieve the TextBlock using FindName().

If your application does not need to do this extra stuff and you only want to change the look and feel of the controls, just start from the default template. If you have Expression Blend, here's a blog on how it can be accomplished: http://blogs.esri.com/Dev/blogs/silverlightwpf/archive/2010/05/20/Use-control-templates-to-customize...


Thanks Jennifer. In fact I want to change the unit too. If the end value of the scalebar is smaller than 10miles, then change the unit from miles to feet. If the end value is bigger than 52800feet, then change the unit from feet to miles.

So ScalebarValue_Loaded is necessary right?
ScalebarValue_Loaded is for the end value of the scalebar.
ScalebarValue1_Loaded, ScalebarValue2_Loaded are for the partial values of the scalebar. Say the end value is 100miles, then the partial values are 20 and 60.
0 Kudos
JenniferNery
Esri Regular Contributor
I suggest you do the following first before customizing the style.
1. Use ExpressionBlend to get the default style (http://blogs.esri.com/Dev/blogs/silverlightwpf/archive/2010/05/20/Use-control-templates-to-customize...)
2. Use FindName() to get the current scalebar value as suggested in post #5 (http://forums.arcgis.com/threads/15157-Access-ScaleBars-Current-Value)
3. Subscribe to your map's ExtentChanged and retrieve the value as suggested in post #6 in the same thread as above
4. Perform your own calculation of getting the partial values (I'm not exactly sure how you arrived at the formula,  currentvalue * 2 /10 or currentvalue * 6 /10) but check that they are correct.
5. If the default style and the calculation works fine, update the style to include your 2 additional TextBlocks.

I think breaking it down into steps/parts, will help you find why the scalebar did not load in the first place.
0 Kudos
DanDong
Deactivated User
I suggest you do the following first before customizing the style.
1. Use ExpressionBlend to get the default style (http://blogs.esri.com/Dev/blogs/silverlightwpf/archive/2010/05/20/Use-control-templates-to-customize...)
2. Use FindName() to get the current scalebar value as suggested in post #5 (http://forums.arcgis.com/threads/15157-Access-ScaleBars-Current-Value)
3. Subscribe to your map's ExtentChanged and retrieve the value as suggested in post #6 in the same thread as above
4. Perform your own calculation of getting the partial values (I'm not exactly sure how you arrived at the formula,  currentvalue * 2 /10 or currentvalue * 6 /10) but check that they are correct.
5. If the default style and the calculation works fine, update the style to include your 2 additional TextBlocks.

I think breaking it down into steps/parts, will help you find why the scalebar did not load in the first place.


Hi Jennifer, thanks for the suggestions, I will try these.
0 Kudos