MVVM Windows Phone 8 - adding a collection of pushpins to a map

前端 未结 2 1229
我在风中等你
我在风中等你 2020-12-15 13:07

Here is the XAML code:



        
相关标签:
2条回答
  • 2020-12-15 13:32

    The MapItemsControl is currently not yet MVVM bindable ( what I am aware off ). So best way is to set it's ItemsSource in the code behind of your view.

    You can still use the collection defined in your ViewModel though! Options are:

    • through mvvm messaging pass along the collection from the viewmodel to the code behind of the view
    • use the datacontext of the view to access the collection, something like this: this.StoresMapItemsControl.ItemsSource = ServiceLocator.Current.GetInstance<MainViewModel>().Locations;
    0 讨论(0)
  • 2020-12-15 13:34

    I finally make it work by using dependency property. I added a new class:

    public static class MapPushPinDependency
    {
        public static readonly DependencyProperty ItemsSourceProperty =
                DependencyProperty.RegisterAttached(
                 "ItemsSource", typeof(IEnumerable), typeof(MapPushPinDependency),
                 new PropertyMetadata(OnPushPinPropertyChanged));
    
        private static void OnPushPinPropertyChanged(DependencyObject d,
                DependencyPropertyChangedEventArgs e)
        {
            UIElement uie = (UIElement)d;
            var pushpin = MapExtensions.GetChildren((Map)uie).OfType<MapItemsControl>().FirstOrDefault();
            pushpin.ItemsSource = (IEnumerable)e.NewValue;
        }
    
    
        #region Getters and Setters
    
        public static IEnumerable GetItemsSource(DependencyObject obj)
        {
            return (IEnumerable)obj.GetValue(ItemsSourceProperty);
        }
    
        public static void SetItemsSource(DependencyObject obj, IEnumerable value)
        {
            obj.SetValue(ItemsSourceProperty, value);
        }
    
        #endregion
    }
    

    And in the .xaml file I have added

    xmlns:dp="clr-namespace:Treks.App.Util.DependencyProperties"
    

    and now the .xaml file looks like this:

    <maps:Map x:Name="NearbyMap" 
                      Center="{Binding MapCenter, Mode=TwoWay}"
                      ZoomLevel="{Binding ZoomLevel, Mode=TwoWay}"
                      dp:MapPushPinDependency.ItemsSource="{Binding Path=Treks}"
                  >
            <maptk:MapExtensions.Children>
                <maptk:MapItemsControl Name="StoresMapItemsControl">
                    <maptk:MapItemsControl.ItemTemplate>
                        <DataTemplate>
                            <maptk:Pushpin x:Name="PushPins" GeoCoordinate="{Binding Location}" Visibility="Visible" Content="test"/>
                        </DataTemplate>
                    </maptk:MapItemsControl.ItemTemplate>
                </maptk:MapItemsControl>
                <maptk:UserLocationMarker x:Name="UserLocationMarker" Visibility="Visible" GeoCoordinate="{Binding MyLocation}"/>
            </maptk:MapExtensions.Children>
        </maps:Map>
    

    Now all the pushpins are correctly rendered.

    0 讨论(0)
提交回复
热议问题