Tim
Tim

Reputation: 1769

Binding with Map API Extensions for Windows Phone 8

I'm trying to use databinding with the map api extension of windows phone toolkit. I'm doing :

<maps:Map x:Name="Map" Center="47.6, -122.3" ZoomLevel="12">
    <maptk:MapExtensions.Children>
        <maptk:MapItemsControl ItemsSource="{Binding PositionList}">
            <maptk:MapItemsControl.ItemTemplate>
                <DataTemplate>
                    <maptk:Pushpin GeoCoordinate="{Binding}" />
                </DataTemplate>
            </maptk:MapItemsControl.ItemTemplate>
        </maptk:MapItemsControl>
    </maptk:MapExtensions.Children>
</maps:Map>

with my code behind :

public partial class MainPage : PhoneApplicationPage, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(string name)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(name));
    }

    private bool NotifyPropertyChanged<T>(ref T variable, T valeur, [CallerMemberName] string name= null)
    {
        if (object.Equals(variable, valeur)) return false;

        variable = valeur;
        NotifyPropertyChanged(name);
        return true;
    }

    private IEnumerable<GeoCoordinate> positionList;
    public IEnumerable<GeoCoordinate> PositionList
    {
        get { return positionList; }
        set { NotifyPropertyChanged(ref positionList, value); }
    }

    public MainPage()
    {
        InitializeComponent();
        PositionList = new List<GeoCoordinate> 
        { 
             new GeoCoordinate(47.6050338745117, -122.334243774414),
             new GeoCoordinate(47.6045697927475, -122.329885661602),
             new GeoCoordinate(47.605712890625, -122.330268859863),
             new GeoCoordinate(47.6015319824219, -122.335113525391),
             new GeoCoordinate(47.6056594848633, -122.334243774414)
        };

        DataContext = this;
    }
}

But I can't see any pushpin on the map :(

What am I doing wrong ?

Note that If I use this in the code-behind file, it's working

MapExtensions.GetChildren(Map).OfType<MapItemsControl>().First().ItemsSource = PositionList;

Thanks in advance for your help,

Best regards

Upvotes: 4

Views: 1766

Answers (1)

jlspublic
jlspublic

Reputation: 143

MapItemsControl derives from DependencyObject, not FrameworkElement so the DataContext does not propagate. Long story long... you can't data bind MapItemsControl from XAML unless you have some way to set the Source property of the Binding.

If the FindAncestor mode of RelativeSource worked on the phone, it might be possible to work around this but it apparently does not. This leaves us with either creating the binding in code or (more realistically) setting the ItemsSource in code.

Upvotes: 4

Related Questions