user2915962
user2915962

Reputation: 2711

XAML data.binding - how can I bind properties in my method to the longitude/latitude in my view?

I'm using GeoLocator to find out my position. This is the method:

private async void GetCurrentLocation()
{
    Geolocator locationFinder = new Geolocator
    {
        DesiredAccuracyInMeters = 50,
        DesiredAccuracy = PositionAccuracy.Default
    };

    try
    {
        Geoposition currentLocation = await  locationFinder.GetGeopositionAsync(
            maximumAge: TimeSpan.FromSeconds(120),
            timeout: TimeSpan.FromSeconds(10));
        String longitude = currentLocation.Coordinate.Longitude.ToString("0.00");
        String latitude = currentLocation.Coordinate.Latitude.ToString("0.00");
        MyTextBlock.Text = "Long: " + longitude + "Lat: " + latitude;
    }
    catch (UnauthorizedAccessException)
    {
        MyTextBlock.Text = "some message";
    }
}

Now, I'm my view I have this code (here with hard-coded values that I would like to replace with longitude/latitude from the above method).

<bm:Map ZoomLevel="7.5" Credentials="my key" x:Name="myMap">
    <bm:Map.Center>
        <bm:Location Latitude="48" Longitude="-122.580489" />
    </bm:Map.Center>
</bm:Map>

I'm very new to this kind of developing and would like to know how I can bind the properties in the method to the long/lat in my view. Thank you!

Upvotes: 1

Views: 210

Answers (1)

furkle
furkle

Reputation: 5059

This would require some sort of object (usually called a ViewModel) from which your Map control would get its DataContext. (DataContext moves down the visual tree, from parent to child, unless explicitly set on any child control.) The ViewModel exposes information you'd like the view (your Window/other visible control) to visually represent somehow to the user.

Note that one of the crucial components of ViewModels in the MVVM style for WPF is that it implements INotifyPropertyChanged. This interface allows an object to signal to the UI that the a value which is bound in the above fashion has changed, and the UI should be updated to reflect that.

Here's a really simple implementation of a ViewModel for your purposes:

// uses System.ComponentModel
public class YourViewModel : INotifyPropertyChanged
{
    private double longitude;
    public double Longitude
    { 
        get { return longitude; }
        set
        {
            longitude = value;
            NotifyPropertyChanged("Longitude");
        }
    } 

    private double latitude;
    public double Latitude
    { 
        get { return latitude; }
        set
        {
            latitude = value;
            NotifyPropertyChanged("Latitude");
        }
    } 

    public event PropertyChangedEventHandler PropertyChanged;
    protected void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

This exposes a Longitude property and a Latitude property which you can use for binding in your XAML. If you're going to do true MVVM, those properties should probably be contained within a model object, and the ViewModel should in turn expose a collection of model objects to the View. As it stands, this is basically as simple an example as possible.

In general, you'd just use a simple binding to connect a control's property to your ViewModel's property, like so:

<TextBlock Text="{Binding Longitude}" />

where the TextBlock's DataContext had been set to an instance of YourViewModel.

However, it looks like the Bing Map object isn't a FrameworkElement, so it doesn't have a DataContext property, and binding is a little more complicated because it needs StaticResources. I don't really have the capacity to test out the Bing maps API, but this link on binding to non-FrameworkElements should help.

Upvotes: 2

Related Questions