2011-05-12 4 views
5

WPF DataGrid에 실제로 표시되는 행 수를 알고 싶습니다.DataGrid에서 보이는 행 수를 계산하십시오.

나는 DataGridRow 이상 반복하고 IsVisible을 확인했지만, 그들이 DataGrid 뷰포트에없는 경우에도 행이 IsVisible = true를보고 할 것으로 보인다.

보이는 행 수를 올바르게 계산하려면 어떻게해야합니까?

답변

1

dataGrid.GetContainerFromItem(dataGrid.Items[row]); 

희망이 도움이 good answer :

나는 그들이 weren 히 경우에도 행이 Visible = true으로 보여주는 같은 문제가 있었다
private bool IsUserVisible(FrameworkElement element, FrameworkElement container) { 
    if (!element.IsVisible) 
     return false; 
    Rect bounds = element.TransformToAncestor(container).TransformBounds(new Rect(0.0, 0.0, element.ActualWidth, element.ActualHeight)); 
    Rect rect = new Rect(0.0, 0.0, container.ActualWidth, container.ActualHeight); 
    return rect.Contains(bounds.TopLeft) || rect.Contains(bounds.BottomRight); 
} 
0

마음에 와서 간단한 해킹,

모든 행을 통해 루프 항목이 용기를 가지고 있는지 확인? 내가 MSDN 포럼에서이 같은 질문을하고있어

+1

이것은 작동하지 않습니다. 사실, 제 루프에서'ItemContainerGenerator.ContainerFromIndex'를 사용하고 있고 표시되지 않은 일부 아이템에는 여전히 컨테이너가 있습니다. –

1

'티.

해결 방법을 찾으려고 할 때 나는이 질문을 게시했습니다 : Visible rows in DataGrid is off by 1 (counted using ContainerFromItem). 여기

은 나를 위해 일한 내용은 다음과 같습니다

자세한 지침은
uint VisibleRows = 0; 
var TicketGrid = (DataGrid) MyWindow.FindName("TicketGrid"); 

foreach(var Item in TicketGrid.Items) { 
    var Row = (DataGridRow) TicketGrid.ItemContainerGenerator.ContainerFromItem(Item); 

    if(Row != null) { 
     if(Row.TransformToVisual(TicketGrid).Transform(new Point(0, 0)).Y + Row.ActualHeight >= TicketGrid.ActualHeight) { 
      break; 
     } 

     VisibleRows++; 
    } 
} 

, 링크 된 질문에 대답을 주도 질문 자체에 대한 사용자의 의견뿐만 아니라 스레드 내 대답에 약간의 /* comments */이 있습니다 .

관련 문제