This is a migrated thread and some comments may be shown as answers.

Setting the MapPinPoint Style

1 Answer 171 Views
Map
This is a migrated thread and some comments may be shown as answers.
LRomano
Top achievements
Rank 1
LRomano asked on 11 Feb 2010, 04:13 PM

I have followed the Telerik sample, which are great by the way, and I'm trying to set the Style of the MapPinPoint to a style defined in the XAML file but I keep getting an error:
System.Windows.Markup.XamlParseException occurred
  Message=" [Line: 0 Position: 0]"
  LineNumber=0
  LinePosition=0
  StackTrace:
       at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
       at MS.Internal.XcpImports.SetValue(INativeCoreTypeWrapper obj, DependencyProperty property, DependencyObject doh)
       at System.Windows.DependencyObject.SetValue(DependencyProperty property, DependencyObject doh)
       at System.Windows.FrameworkElement.set_Style(Style value)
       at MapApplication.MainPage.DisplayKMLFileUsingTelerikLoad()
  InnerException:

The XAML style is defined as:

                            <Style x:Key="bridgeStyle" TargetType="ContentControl">  
                                <Setter Property="FontWeight" Value="ExtraBold"/>  
                                <Setter Property="FontSize" Value="14"/>  
                                <Setter Property="Background" Value="White"/>  
                            </Style> 

and it is part of the RapMap.Resources section:

 
                    <map:RadMap Grid.Row="0" Margin="0,5,0,0" x:Name="RadMap1" UseDefaultLayout="True"   
                        UseSpringAnimations="False" 
                        MouseClickMode="None" 
                        MapMouseClick="radMap_MapMouseClick" 
                        MapMouseDoubleClick="radMap_MapMouseDoubleClick">  
                        <map:RadMap.Resources> 
                            <ControlTemplate x:Key="callerTemplate" TargetType="ContentControl">  
                                <Ellipse Width="10" Height="10" Fill="Orange" Stroke="White" StrokeThickness="1"/>  
                            </ControlTemplate> 
 
                            <Style x:Key="callerStyle" TargetType="ContentControl">  
                                <Setter Property="VerticalAlignment" Value="Center" /> 
                                <Setter Property="HorizontalAlignment" Value="Center" /> 
                                <Setter Property="Template" Value="{StaticResource callerTemplate}" /> 
                            </Style> 
                            <Style x:Key="bridgeStyle" TargetType="ContentControl">  
                                <Setter Property="FontWeight" Value="ExtraBold"/>  
                                <Setter Property="FontSize" Value="14"/>  
                                <Setter Property="Background" Value="White"/>  
                            </Style> 
                        </map:RadMap.Resources> 
                        <maps:InformationLayer x:Name="informationLayer">  
                        </maps:InformationLayer> 
                    </map:RadMap> 

The code is based on the Telerik samples where a KML file is loaded using the KmlReader and the items are looped over via FrameworkElement objects. Here is the part where MapPinPoints are being set with the Style and where the error occurs:
List<FrameworkElement> list = KmlReader.Read(streamInfo.Stream);  
 
            foreach (FrameworkElement element in list)  
            {  
                //Label Text is not picking up the KML style so use a Silverlight one  
                MapPinPoint pinPoint = element as MapPinPoint;  
                if (pinPoint != null)  
                {  
                    //set the Silverlight style, using an inline style defined in the RadMap section  
                    ContentControl tempControl = new ContentControl();  
                    tempControl.Style = RadMap1.Resources[defaultStyle] as Style;  
                    pinPoint.Style = tempControl.Style; //ERROR HERE  
                }  
            }  
 

Anything I try gives me an error but if the set the MapPinPoint properties directly it works: e.g.
    pinPoint.FontSize = 14;
    ....

This this just an issue with CTP or am I doing something wrong?
EDIT: I found this sample but it doesn't show the "code" version of how you would do the same thing in the code-behind and thats what I am looking for. http://http//www.telerik.com/help/silverlight/map-adding-pin-points.html

Thanks,

Leo

1 Answer, 1 is accepted

Sort by
0
LRomano
Top achievements
Rank 1
answered on 11 Feb 2010, 08:46 PM
Yes I know I am replying to my own post but maybe it will help someone else out. The issue comes down to knowing Silverlight templates, styles, and namespaces... which clearly I don't. :-)

If you change the XAML styles "TargetType" to "layer:MapPinPoint" as such:
                            <Style x:Key="bridgeStyle" TargetType="layer:MapPinPoint">  
                                <Setter Property="FontWeight" Value="ExtraBold"/>  
                                <Setter Property="FontSize" Value="10"/>  
                                <Setter Property="Foreground" Value="White"/>  
                                <Setter Property="Background" Value="Black"/>  
                                <Setter Property="Opacity" Value="0.75"/>  
                            </Style> 
Then you can cast the Style to a MapPinPoint's Style property, as such:
                MapPinPoint pinPoint = element as MapPinPoint;  
                if (pinPoint != null)  
                {  
                    //set the Silverlight style, using an inline style defined in the RadMap section  
                    pinPoint.Style = RadMap1.Resources["bridgeStyle"] as Style;   
                }  
 
The code above is used in several Telerik demos where they load a list of items from a KML file and loop over the FrameworkElement items doing something to them i.e. change the color, etc. I wanted to define a Style that could be applied without have to set all the different properties and this works in Silverlight 3. I don't know about Silverlight 2 and I have read you can only apply styles once to a control in SL2.

So the key is use the correct XAML namespaces to get at your control. The following example is what I first had and I could figure out why it didn't work but COMPILES FINE:
                            <Style x:Key="bridgeStyle" TargetType="MapPinPoint">     
                                <Setter Property="FontWeight" Value="ExtraBold"/>     
                                <Setter Property="FontSize" Value="10"/>     
                                <Setter Property="Foreground" Value="White"/>     
                                <Setter Property="Background" Value="Black"/>     
                                <Setter Property="Opacity" Value="0.75"/>     
                            </Style>    
 
NOTE that doesn't have correct Namespace so Silverlight can find the control but it only checks during runtime. In most of the Telerik Help samples you'll see a namespace declaration as the top of the page such as:
    xmlns:layer="clr-namespace:Telerik.Windows.Controls.Map;assembly=Telerik.Windows.Controls.DataVisualization" 
 
and this is the one of keys to getting everything to work.

Hope this helps someone else!!

Leo
Tags
Map
Asked by
LRomano
Top achievements
Rank 1
Answers by
LRomano
Top achievements
Rank 1
Share this question
or