Answered XAML vs WPF code for pushpin

  • Friday, March 23, 2012 6:54 PM
     
     

    I have the following definition in the XAML for a user control:


            <m:Map CredentialsProvider="MY-KEY-HERE"
                   Name="myMap" Width="377">
            <m:Pushpin Name="testPin" Location="37.585942, -121.908417" >
                <TextBlock TextWrapping="Wrap" Name="PushpinTag" Text="456" />
            </m:Pushpin>
        </m:Map>

    It creates a nice puspin with an embedded label. I can change the label and/or the locatoin from code. Fine so far.

    What I'd like to do is to create such pushpins from within the codebehind page - or to make a copy of this pin so I can add it to the map as well. I'm farily new at this, so I may be missing something, but the PushPin class doesn't seem to have any way to embed other controls, and I haven't seen how to (on the fly) create a nested control like this.

    Any suggestions?

All Replies

  • Saturday, March 24, 2012 4:42 PM
     
     Answered Has Code

    As I understood, you need a code, which will create such Pushpins, during running the program. I'm writing in C#, so this is a code in C# to do it:

    Pushpin testPin = new Pushpin(); // create a Pushpin
    TextBlock pushpinTag = new TextBlock(); // create a textblock
    pushpinTag.Text = "456"; // assign text to textblock
    testPin.Content = PushpinTag; // insert textblock into pushpin
    testPin.Location = new Location(37.585942, -121.908417); // set location for pushpin
    myMap.Children.Add(testPin); // add pushpin to the map

    Or, if you need only text\numbers, you can avoid TextBlock and simply insert string into Pushpin.Content:

    testPin.Content = "456";
    • Marked As Answer by HJ H Monday, March 26, 2012 4:29 PM
    •  
  • Sunday, March 25, 2012 10:35 AM
    Owner
     
     Answered

    What you can do instead is create a user control and add that to the map and use is as a pushpin, rather than trying to edit the pushpin class itself. Take a look at this article: http://msdn.microsoft.com/en-us/library/hh868032.aspx


    http://rbrundritt.wordpress.com

    • Marked As Answer by HJ H Monday, March 26, 2012 4:35 PM
    •  
  • Monday, March 26, 2012 4:29 PM
     
     

    Ah - didn't know that was how to use the "Content" property! Duh. Thanks.

  • Monday, March 26, 2012 4:35 PM
     
     

    This also works, but is more complex than I need (at least for now?). I'd seen the article, but am still finding my way between XAML and code....

    Implementing the specialized map items as UserControls is an idea I may adopt. (Actually, given that this WPF map is going to be used by Windows code, I'm already implementing it as a UserControl.)

    Thanks!