4

Windows Phone ツールキットのマップ API 拡張機能でデータバインディングを使用しようとしています。私がやっている :

<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>

私のコードビハインドで:

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;
    }
}

しかし、地図上に画鋲が見当たりません:(

私は何を間違っていますか?

コードビハインドファイルでこれを使用すると、機能することに注意してください

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

よろしくお願いいたします。

よろしくお願いします

4

1 に答える 1

4

MapItemsControl は FrameworkElement ではなく DependencyObject から派生するため、DataContext は伝達されません。長い話ですが、Binding の Source プロパティを設定する方法がない限り、XAML から MapItemsControl をデータ バインドすることはできません。

RelativeSource の FindAncestor モードが電話で機能した場合、これを回避できる可能性がありますが、そうではないようです。これにより、コードでバインディングを作成するか、(より現実的には) コードで ItemsSource を設定する必要があります。

于 2013-02-27T15:00:36.177 に答える