I am trying to reproduce the BingMaps sample of the Windows Phone 7 trainingkit: http://msdn.microsoft.com/en-us/wp7trainingcourse_usingbingmapslab_topic2.aspx#_Toc271039352
but instead of wiring everything in codebehind i'd like to use a viewmodel.
Everything works fine except binding to the Mode property (aerial or road) causes a XamlParseException. Is there a problem because it isn't a simple property?
This is the original Xaml:
<my:Map Name="Map"
CredentialsProvider="{Binding CredentialsProvider}">
<my:Map.Mode>
<my:AerialMode ShouldDisplayLabels="True" />
</my:Map.Mode>
</my:Map>
The Map.Mode can be changed from codebehind.
Instead I am trying the following:
<my:Map x:Name="Map"
CredentialsProvider="{Binding CredentialsProvider}"
ZoomLevel="{Binding Zoom, Mode=TwoWay}"
Center="{Binding Center, Mode=TwoWay}"
Mode="{Binding MapMode}" />
and the important part of the viewmodel:
private MapMode _mapMode = new AerialMode(true);
public MapMode MapMode
{
get { return _mapMode; }
set
{
_mapMode = value;
RaisePropertyChanged("MapMode");
}
}
private void ChangeMapMode()
{
if (MapMode is AerialMode)
{
MapMode = new RoadMode();
}
else
{
开发者_开发百科 MapMode = new AerialMode(true);
}
}
Thanks for your help!
Solved.
"Mode" isn't a dependency property. So it cannot be bound.
My workaround:
- added dependency property to view (=Page)
- bound dependency property to property in viewmodel (via code in the constructor)
Set Mode of Map control in the propertyChanged callback handler
//Constructor public MainPage() { InitializeComponent(); DataContext = new MainViewModel(); Binding b = new Binding("MapMode"); this.SetBinding(MapModeProperty, b); } //DependencyProperty. No need for corresponding CLR-property. public static readonly DependencyProperty MapModeProperty = DependencyProperty.Register("MapMode", typeof(MapMode), typeof(MainPage), new PropertyMetadata(OnMapModeChanged)); //Callback private static void OnMapModeChanged(DependencyObject element, DependencyPropertyChangedEventArgs e) { ((MainPage)element).Map.Mode = e.NewValue as MapMode; }
Hope this one will help others!
I suspect you'll need to use a converter with your binding.
精彩评论