2

画鋲を MapLayer にデータ バインドしています。それらは正常に表示されますが、relaycommand を使用してマウスの leftbuttonUp からイベント引数を渡すと、オブジェクト ソースは楕円になります。MapPolygon でこのメソッドを使用して、必要な情報をオブジェクトから取得しました。

私はmvvmが初めてなので、これにひどく近づいているかもしれませんので、お知らせください!

これは私のMapPolygonsで機能します(vmはextendedMapPolygonクラスの名前空間を参照します)

    <DataTemplate x:Name="polyTemplate">
        <vm:extendedMapPolygon cName="{Binding _cName}" Locations="{Binding _Locations}" />
    </DataTemplate>

MapLayer の XAML は次のとおりです。

    <m:MapItemsControl ItemTemplate="{StaticResource polyTemplate}" ItemsSource="{Binding  Path=_PolyforDisplay, Mode=TwoWay}"  >
        <i:Interaction.Triggers>
               <i:EventTrigger EventName="MouseLeftButtonUp">
                    <cmd:EventToCommand Command="{Binding Path=PolySelCommand}" PassEventArgsToCommand="True" ></cmd:EventToCommand>
               </i:EventTrigger>
        </i:Interaction.Triggers>
    </m:MapItemsControl>

私のviewModelコンストラクターで

PolySelCommand = new RelayCommand<MouseButtonEventArgs>(PolySelCommandExecute);

そして最後に実際のコマンド

        public RelayCommand<MouseButtonEventArgs> PolySelCommand { get; set; }
    private void PolySelCommandExecute(MouseButtonEventArgs cmp)
    {
        Polygon poly = cmp.OriginalSource as Polygon;
        extendedMapPolygon ePoly = poly.Parent as extendedMapPolygon;
        _MapPolygonSelected =  ePoly.cName;
    }

(私が現在使用している方法と、他の人に役立つことを願って、これをここに置きます!)

ただし、プッシュピンで同じことを試みると、cmp.OriginalSource は楕円であり、他に何も渡されないようです。

私の画鋲コード (このコードでは、MapControl で画鋲を使用しているだけです)

    <DataTemplate x:Name="ppTemplate">
        <m:Pushpin ToolTipService.ToolTip="{Binding _psName}" Location="{Binding _Location}" />
    </DataTemplate>

    <m:MapItemsControl ItemTemplate="{StaticResource ppTemplate}" ItemsSource="{Binding Path=_PinsforDisplay, Mode=TwoWay}">
        <i:Interaction.Triggers>
             <i:EventTrigger EventName="MouseLeftButtonUp">
                  <cmd:EventToCommand Command="{Binding Path=pinSelCommand}" PassEventArgsToCommand="True" ></cmd:EventToCommand>
             </i:EventTrigger>
        </i:Interaction.Triggers>
    </m:MapItemsControl>

コマンド パラメータを使用する必要がありますか? または、画鋲をクリックしたときにビューモデルにテキストを渡す別の方法です。これが実際に必要です。

4

1 に答える 1

5

トリガーを押しピン要素に移動します。

    <DataTemplate x:Name="ppTemplate">
        <m:Pushpin ToolTipService.ToolTip="{Binding _psName}" Location="{Binding _Location}">
             <i:Interaction.Triggers>
                    <i:EventTrigger EventName="MouseEnter">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="MouseLeftButtonUp">
                                <cmd:EventToCommand Command="{Binding Path=DataContext.pinSelCommand}" 
                                                    CommandParameter="{Binding WhateverPropertyHasTheText" />
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
        </m:Pushpin>
    </DataTemplate>

_PinsForDisplay 内のオブジェクトから送信するプロパティをコマンド パラメーターとして渡していることに注意してください。また、バインディングがデータ テンプレート内とは異なるため、コマンドのバインディングがわずかに変更されました。

そして、ビューモデルの RelayCommand を RelayCommand に変更する必要があります。

私はこのコードをテストしませんでしたが、非常によく似たものが機能しているので、解決策につながることを願っています.

于 2011-05-26T20:36:49.887 に答える