How can I bind Pushpin location in WPF using MVVM? -
i'm developing desktop application uses bing maps , mvvm.
in application, user adds pushpin in map double clicking on it, pushpin location gets saved in event class , event class sent through wcf service.
i latitude , longitude pushpin using data binding, compiler complains dependencyproperty when try that. managed set latitude , longitude in viewmodel view, don't know if it's valid in mvvm. have seen examples using mapsitemcontrols don't understand them.
viewmodel
private event evt; public event evt { { return this.evt; } set { this.evt = value; onpropertychanged("event"); } }
map xaml
<m:map grid.rowspan="5" grid.column="3" margin="3" name="operatormap" credentialsprovider="map_key" center="19.4000,-99.1333" zoomlevel="5" mousedoubleclick="setpushpinlocation" />
codebehind
private maintenanceformviewmodel viewmodel = new maintenanceformviewmodel(); private pushpin pin = null; public mainwindow() { initializecomponent(); this.loaded += (s, e) => { this.datacontext = this.viewmodel; }; } private void setpushpinlocation(object sender, mousebuttoneventargs e) { e.handled = true; point mouseposition = e.getposition((uielement)sender); location pinlocation = operatormap.viewportpointtolocation(mouseposition); if (pin == null) { pin = new pushpin(); operatormap.children.add(pin); } pin.location = pinlocation; this.viewmodel.evt.latitude = pinlocation.latitude; this.viewmodel.evt.longitude = pinlocation.longitude; }
bing maps uses attached property maplayer.position
positioning elements on map.
given view model property of type location
public class viewmodel : inotifypropertychanged { private location location; public location location { { return location; } set { location = value; onpropertychanged("location"); } } ... }
you can bind position of pushpin this:
<bm:pushpin bm:maplayer.position="{binding location}"/>
note in windows store app version of bing maps sdk there seems a bug when try setup binding in xaml. says (still bing maps sdk version 1.313.825.0)
failed assign property 'bing.maps.maplayer.position'
you can create binding in code behind:
pushpin.setbinding(maplayer.positionproperty, new binding { path = new propertypath("location") });
Comments
Post a Comment