2011-09-14 2 views
1

Silverlight 데이터 표가 있으면 특정 데이터 컨텍스트에서 해당 데이터 표 행을 어떻게 찾을 수 있습니까?Silverlight : 데이터 컨텍스트로 DataGrid에서 데이터 격자 행을 가져 오는 방법

public DataGridRow GetDataGridRowByDataContext(DataGrid dataGrid, object dataContext) 
{ 
    if (null != dataContext) 
    { 
     dataGrid.ScrollIntoView(dataContext, null); 

     DataGridAutomationPeer automationPeer = (DataGridAutomationPeer)DataGridAutomationPeer.CreatePeerForElement(dataGrid); 

     // Get the DataGridRowsPresenterAutomationPeer so we can find the rows in the data grid... 
     DataGridRowsPresenterAutomationPeer dataGridRowsPresenterAutomationPeer = automationPeer.GetChildren(). 
      Where(a => (a is DataGridRowsPresenterAutomationPeer)). 
      Select(a => (a as DataGridRowsPresenterAutomationPeer)). 
      FirstOrDefault(); 

     if (null != dataGridRowsPresenterAutomationPeer) 
     { 
      foreach (var item in dataGridRowsPresenterAutomationPeer.GetChildren()) 
      { 
       // loop to find the DataGridCellAutomationPeer from which we can interrogate the owner -- which is a DataGridRow 
       foreach (var subitem in (item as DataGridItemAutomationPeer).GetChildren()) 
       { 
        if ((subitem is DataGridCellAutomationPeer)) 
        { 
         // At last -- the only public method for finding a row.... 
         DataGridRow row = DataGridRow.GetRowContainingElement(((subitem as DataGridCellAutomationPeer).Owner as FrameworkElement)); 

         // check this row to see if it is bound to the requested dataContext. 
         if ((row.DataContext) == dataContext) 
         { 
          return row; 
         } 

         break; // Only need to check one cell in each row 
        } 
       } 
      } 
     } 
    } 

    return null; 
} 
: 여기

답변

4

내가이 다양한 automationpeers을 활용해야 할 쓴 방법이다
관련 문제