locked
HubSection Bing Maps control code behind, add shaplayers RRS feed

  • Question

  • How can I get access from code behind to an element defined in a HubSection Content Template?

    I need to add layers to the Bing Map control (myMap) from code behind

    <HubSection.ContentTemplate>

    <DataTemplate>

    <Maps:Map x:Name="myMap" />

    </DataTemplate>

    </HubSection.ContentTemplate>

    </HubSection>

    I tried this, but the map does not get refreshed:

    var myTemplate = mySection.ContentTemplate;

    var sp = myTemplate.LoadContent();

    ((Map)sp).ShapeLayers.Add(xxxLayer);

    What am I missing?

    Wednesday, July 10, 2013 10:52 AM

Answers

  • There are two basic approaches that you can use here:

    1) Use a UserControl to encapsulate the content for your HubSection.  In the code behind of your UserControl you will be able to reference your elements as usual.

    2) A less desired technique is to hook the Loaded event for the elements that you want to have a reference to and then use the 'sender' argument as that reference.

    This happens because of scoping issues due to the fact that we are using DataTemplates for the HubSection content which is done for performance reasons.

    • Marked as answer by alarranaga Friday, July 12, 2013 9:03 AM
    Thursday, July 11, 2013 5:47 PM

All replies

  • How can I get access from code behind to an element defined in a HubSection Content Template?

    I need to add layers to the Bing Map control (myMap) from code behind

    <HubSection>

    <HubSection.ContentTemplate>

    <DataTemplate>

    <Maps:Map x:Name="myMap" />

    </DataTemplate>

    </HubSection.ContentTemplate>

    </HubSection>

    Wednesday, July 10, 2013 7:31 AM
  • <HubSection x:Name="mySection">

    <HubSection.ContentTemplate>

    <DataTemplate>

    <Maps:Map x:Name="myMap" />

    </DataTemplate>

    </HubSection.ContentTemplate>

    </HubSection>

    I tried this, but the map does not get refreshed:

    var myTemplate = mySection.ContentTemplate;

    var sp = myTemplate.LoadContent();

    ((Map)sp).ShapeLayers.Add(xxxLayer);

    What am I missing?

    Wednesday, July 10, 2013 9:42 AM
  • Rather than trying to directly manipulate the contents of the DataTemplate I'd put the map in a user control and host the user control in the DataTemplate. That will allow you to do all of the event hook ups and programmatic access to the Map that you need without worrying about how the hub is virtualizing the section.

    I'd only put raw elements directly in the DataTemplate if everything can be hooked up in the Xaml with data binding.

    --Rob

    Thursday, July 11, 2013 4:01 AM
    Moderator
  • There are two basic approaches that you can use here:

    1) Use a UserControl to encapsulate the content for your HubSection.  In the code behind of your UserControl you will be able to reference your elements as usual.

    2) A less desired technique is to hook the Loaded event for the elements that you want to have a reference to and then use the 'sender' argument as that reference.

    This happens because of scoping issues due to the fact that we are using DataTemplates for the HubSection content which is done for performance reasons.

    • Marked as answer by alarranaga Friday, July 12, 2013 9:03 AM
    Thursday, July 11, 2013 5:47 PM