Oct 15, 2010

WPF DataGrid Controlling the Scrollbar

I just started with WPF controls and one of the issues I came through is controlling the scrollbar in WPF objects. In WPF you have logical and visual trees. To get the scrollbar, Microsoft recommendation is to go trough the tree to get the scroll bar. So find my implementation below.

/// Method to get scrollbar in a visual object
private static ScrollViewer GetScrollbar(DependencyObject dep)
{
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dep); i++)
    {
        var child = VisualTreeHelper.GetChild(dep, i);
        if (child != null && child is ScrollViewer)
            return child as ScrollViewer;
        else
        {
            ScrollViewer sub = GetScrollbar(child);
            if (sub != null)
                return sub;
        }
    }
    return null;
}

ScrollViewer scrollView = GetScrollbar(myDataGrid);

It should work with other objects too, but I only tried with a WPF Datagrid.  I lost some time to figure out how to do this, hope it helps.

6 comments:

  1. Thanks for this.. it solved an issue I was having. Bind a ObservableCollection to my Dataset, and populate the collection initially. I never delete or add rows, i only update the values of fields in the rows programmatically. If i try and sort by a column it does so but does not re-sort when i update a value. To force this to occur i have to copy all the SortDescriptions, remove them from the datagrid and then re-add them. I then need to go through each column and assign the sort direction. In doing so.. the scroll bars seem to play tricks and sometimes move on there own. By getting the scroll bar vertical offset before performing the hack and then reassigning the offset after the hack is complete ensures my scroll bars dont move :) Thanks

    ReplyDelete
  2. Thanks! That helped!

    ReplyDelete
  3. Thanks! That helped a lot!

    ReplyDelete
  4. Thank you. I've been trying to figure this out for about 2 hours.

    ReplyDelete
  5. If you’re a winner, have the ability to|you 스마일토토 presumably can} expect any funds to be in your account within hours. Yes, on-line on line casino players win real cash on on-line slots video games all the time basis}. As lengthy as you be a part of real cash on-line casinos or download real cash slots apps, have the ability to|you presumably can} doubtlessly win real cash.

    ReplyDelete
  6. It is a measure to reinforce the speed, durability and cutting capacity of the machine. The Asia Pacific region dominates the worldwide CNC machine Industry in the forecast interval. The regional players Europe, North America and Asia pacific are responsible for the massive Hand Massagers CNC machine market worth of USD ninety Billion in 2026. The surging industrial units and rise in the demand for automation expertise are the essential market drivers. North America is the second-largest CNC machine market in the world. The high demand from the profitable manufacturing sectors is the principle causes for the high demand.

    ReplyDelete