2012-01-20 32 views
2

我增加了一些推平到一定到數據源的地圖。獲取當前項目的數據綁定項

 <my:Map Height="340" HorizontalAlignment="Left" Margin="12,100,0,0" Name="settingsmap" VerticalAlignment="Top" Width="438" CredentialsProvider="_"> 
      <my:MapItemsControl Name="settingsMapItemControl" MouseLeftButtonDown="settingsMapItemControl_MouseLeftButtonDown"> 
       <my:MapItemsControl.ItemTemplate> 
        <DataTemplate> 
         <my:Pushpin Location="{Binding location}"> 
         </my:Pushpin> 
        </DataTemplate> 
       </my:MapItemsControl.ItemTemplate> 
      </my:MapItemsControl> 
     </my:Map> 

推針綁定到dataitem的「位置」成員。

現在,當有人點擊引腳(或觸摸手機)時,我如何獲得綁定到該引腳的項目(從綁定到映射的對象列表中)? 我想基本上獲得該項目,這樣我可以在屏幕底部顯示一個類似名稱等其他信息。或者說像您選擇「XYZ」

回答

1

如果你使用MVVM,把一個EventTrigger和圖釘的InvokeCommandAction,並綁定的DataContext本身的命令參數。

<my:Pushpin Location="{Binding location}"> 
<i:Interactivity.Triggers> 
    <i:EventTrigger EventName="Tap"> 
    <i:InvokeCommandAction Command="{Binding PushpinClick}" 
     CommandParameter="{Binding}" /> 
    </i:EventTrigger> 
    <i:Interactivity.Triggers> 
</my:Pushpin> 

而且只需一個ICommand名爲PusphinClick在你的虛擬機,你會在Execute

如果你不使用MVVM那麼它會得到一個有點棘手的參數獲取數據 - 嘗試的MouseLeftButtonDown事件處理程序連接到圖釘。在事件處理程序中,您可以通過獲取(sender as Pushpin).DataContext作爲YourDataItem來獲取「數據」...

+0

(sender as Pushpin).DataContext => null –

+0

是DataContext null還是發件人不是圖釘? – TDaver

+0

因爲如果圖釘沒有DataContext,那麼'{Binding location}'也不應該工作(除非你從代碼隱藏中做了一些事情,並且不告訴我們... – TDaver