none
Counting the number of visible rows in a DataGrid

    Question

  • I would like to know how many rows are actually displayed by a WPF DataGrid.
    I tried looping over DataGridRow and checking IsVisible, but it seems that rows report IsVisible=true even when they are not in the DataGrid view port.
    Any idea?

    Thanks.

    Sunday, May 15, 2011 1:58 PM

Answers

  • Hi Amir,

    Try using the following code to check IsVisible.

    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);
    }
    


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, May 16, 2011 6:39 AM
    Moderator