开发者

Is there a way to ensure that a Silverlight ScrollViewer keeps the top item completely visible

In Silverlight 4, is there a way that whenever you page down a ScrollViewer (i.e click the scroll bar in the area adjacent to the thumb) whatever item is at the top is completely visible. I still need it to scroll smoothly when the thumb is dragged or the mouse wheel is used.

My client doesn't like that an item is cut in half when he pages down the list because it is cut in half both when its at the top and when its at the bottom. I suggested some sort of integral scroll, and he didn't like it. He wants it to still scroll smoothly unless paging up or down.

Edits

Here's the catch. The items are not the same size. So I have to detect the item that is at the top of the scroll viewer and Scroll it into view. Is there an开发者_如何学编程 easy way to do this?


The first thing you need to do is dig out the vertical scroll bar from the internals of the ScrollViewer. You can do this with the help of VisualTreeHelper. There are a number of little chunks of code in various blogs the make its use even easier. I recommend this VisualTreeEnumeration (but I would wouldn't I). With that extensions class in place you can get the vertical scroll bar with:

ScrolBar vertSB = someScrollViewer.Descendents()
                                  .OfType<ScrollBar>()
                                  .FirstOrDefault(sb => sb.Name = "VerticalScrollBar");

Now you can attach to is Scroll event and determine the type of scroll that occured like this:

vertSB.Scroll += (s, args) =>
{
     if (args.ScrollEventType == ScrollEventType.LargeDecrement
         || args.ScrollEventType == ScrollEventType.LargeIncrement)
     {
          // using args.NewValue determine the correct Integral value and assign
          // using someScrollViewer.ScrollToVerticalOffset
     }
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜