WPF Custom ScrollViewer - ItemHeight on Virtualization

I have a ListBox that uses a custom ScrollViewer (to provide fake "touch" scrolling on an embedded Windows xp touchpad)

<ControlTemplate TargetType="{x:Type auc:DragSortableListView}">
    <auc:DragScrollViewer ...>
        <ItemsPresenter .../>
    </auc:DragScrollViewer>
</ControlTemplate>

      

In this "DragScrollViewer" I am using the IScrollInfo interface to perform scrolling, which works well.

Plus I use UI virtualization because we have large amounts of data tied to the list view and scrolling (when virtualization is enabled) is not pixel-based, but index-based as I found out. This means that if I go to a vertical offset of 5 via IScrollInfo, it will scroll to the 5th element.

My problem is that I don't know how to convert the pixel-mouse offset (when the user moved the 50-peak "mouse") to an offset based on the number of items that IScrollInfo.SetVerticalOffset () expects (offset was 3, item 10pixel => set offset to 8). It would be easy if I knew Item-Height, but I'm inside a ScrollViewer. How can the ScrollViewer know if the ItemsPresenter is in the visual tree, right? And what if the elements have different heights (they are not, but hypothetically)?

Any suggestions on how to solve this problem?

+3


source to share


1 answer


Have you seen this article ?

As I see it, it stores the mouse offset in _Offset and then calls InvalidateArrange (), which can (just guessing here) in turn request the VerticalOffset property and handle the appropriate scrolling.



private Vector _Offset;
public double VerticalOffset  { get { return _Offset.Y; } }

public void SetVerticalOffset(double offset)
{
  offset = Math.Max(0, Math.Min(offset, ExtentHeight - ViewportHeight));
  if (offset != _Offset.Y)
  {
    _Offset.Y = offset;
    InvalidateArrange();
  }
}

      

I guess this will never require you to actually convert the pixel offset to the item counter offset. If this is not correct, please provide your IScrollInfo implementation.

+1


source







All Articles