2017-05-24 91 views
0

之前獲得的行選擇我在DataGrid中得到了我的Image-Row這個XAMLMouseDown事件的Datagrid

private void Row_DoubleClick(object sender, MouseButtonEventArgs e) 
{ 
    Equipment classObj = clientDataGrid.SelectedItem as Equipment; 
    string cellValue = classObj.EQNr; 

    lblArtikel.Content = "Equipment: " + cellValue ; 
    Dispatcher.BeginInvoke((Action)(() => tbControlETK.SelectedIndex = 1)); 
} 

其中實際工作,但現在我現在試着爲這個圖像按鈕獲得相同的功能,但這給了我一個空的異常,因爲MouseDown事件在Row被選擇之前觸發......任何解決方案?

我已經想到了圖像行的功能,我可以得到發件人的行或類似的東西。

回答

1

您可以通過可視化樹搜索DataGridRow進行選擇並使用它們,如果發現這樣的話。

<Image x:Name="imgSettings" Source="img/settings_blue.png" Stretch="None" MouseDown="Mouse_Down" /> 



private void Mouse_Down(object sender, MouseButtonEventArgs e) 
     { 
      DataGridRow gridRowGettingSelection = null; 
      var visParent = VisualTreeHelper.GetParent(sender as FrameworkElement); 
      while (gridRowGettingSelection == null && visParent != null) 
      { 
       gridRowGettingSelection = visParent as DataGridRow; 
       visParent = VisualTreeHelper.GetParent(visParent); 
      } 
      if (gridRowGettingSelection == null) { return; } 

      Equipment classObj = gridRowGettingSelection.DataContext as Equipment; 
      string cellValue = classObj.EQNr; 

      lblArtikel.Content = "Equipment: " + cellValue; 
      Dispatcher.BeginInvoke((Action)(() => tbControlETK.SelectedIndex = 1)); 

     } 
+0

令人驚歎! Exactlty我想要的 –