2014-01-11 66 views
0

我可以很容易地從用下面的代碼點擊的對象中獲取數據源的itemID。但任何人都可以請告訴我如何獲得相同的ItemID當我使用Grid_PointerEntered(object sender,PointerRoutedEventArgs e) event?Metro GridView鼠標懸停哪個綁定項目ID

當您將鼠標放在不同的Gridview項目上時,我試圖播放不同的聲音。聲音文件名來自數據源項目

謝謝。

void ItemView_ItemClick(object sender, ItemClickEventArgs e) 
    { 
     var itemId = ((SampleDataItem)e.ClickedItem).UniqueId; 
     //Do something here with the DatasetItem[ItemID] 
    } 

OK我已經試過這個方法

private async void Grid_PointerEntered(object sender, PointerRoutedEventArgs e) 
    { 

     String atext = "default"; 
     Windows.UI.Xaml.Input.Pointer ptr = e.Pointer; 

     if (ptr.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Mouse) 
     { 
      Windows.UI.Input.PointerPoint ptrPt = e.GetCurrentPoint(null); 

      TextBlock aTextBlock = Helpers.FindChild<TextBlock>(this, "aTittle");  
     } 

的問題,這是對的DataTemplate/datagridview的項目都具有相同的名稱。因此,FindChild只返回GridView中的第一個項目。從事件參數

回答

0

取點位置(見這裏的樣本:http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.input.pointerroutedeventargs.getcurrentpoint),然後用VisualTreeHelper檢查元素的指針下

void GridView_PointerEntered(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e) 
    { 
     GridView gridView = sender as GridView; 
     FrameworkElement root = Window.Current.Content as FrameworkElement; 

     Point position = gridView.TransformToVisual(root).TransformPoint(e.GetCurrentPoint(gridView).Position); 


     // check items directly under the pointer 
     foreach (var element in VisualTreeHelper.FindElementsInHostCoordinates(position, root)) 
     { 
      var context = ((FrameworkElement)element).DataContext; 
     } 
    } 
+0

感謝您的答覆。我已經嘗試了以上,但似乎總是返回零元素。 – ckglobalroaming

+0

也許你會得到不正確的位置。我在我的答案中更新了代碼示例。我在我的本地應用程序中嘗試過,它適用於我。給出正確的項目。請注意,實際結果取決於控制佈局和指針移動。例如,您可以移動鼠標,使PointerEnter在沒有任何項目的空白處被觸發,只是GridView ItemsPanel。 – notacat