0

ListCollectionView has a property get the current position (CurrentPosition):

http://msdn.microsoft.com/en-us/library/system.windows.data.collectionview.currentposition.aspx

But this property is read-only.

Gets the ordinal position of the CurrentItem within the (optionally sorted and filtered) view.

So how can one set the current position? I hope this doesn't involve behaviors...

I need to scroll to the top of a ListView or DataGrid and it seems to me it ought to be possible to easily set a property such as that one to achieve it, without going into the guts of a view.

Thanks in advance.

Update:

This is my own workaround at the moment - but it's kind of disguisting - when one wants to use MVVM (using F#):

let linesControl = context.Window.FindName("ObjectListView") :?> ListView
let scrollBorder = VisualTreeHelper.GetChild(linesControl, 0) :?> Border
let scrollViewer = scrollBorder.Child :?> ScrollViewer
scrollViewer.ScrollToVerticalOffset(0.0)
Bent Rasmussen
  • 5,538
  • 9
  • 44
  • 63
  • Both DataGrid and ListView have a method called `ScrollIntoView`. You have to pass the element you want to scroll to. `grid.ScrollIntoView(grid.Items[0])` could work in your case I guess? – Peter Hansen Mar 22 '13 at 17:43

2 Answers2

2

What I use is basically an attached property to work around the read-only, non-bindable properties (even most of the time you don't need to write, binding fails)..

This article solves similar issue with ActualWidth / ActualHeight...

http://meleak.wordpress.com/2011/08/28/onewaytosource-binding-for-readonly-dependency-property/

...and you can download the code / example there (or http://dl.dropbox.com/u/39657172/Blog/PushBindingInStyleDemo.zip).

What you use to bind is something like...

<pb:PushBindingManager.PushBindings>
    <pb:PushBinding TargetProperty="CurrentPosition" Path="YourPositionProperty"/>
</pb:PushBindingManager.PushBindings>  

I didn't really try it on your example - but by the look of it - that should work the same way if there is CurrentPosition already.


Also, take a look at this earlier post for more details if needed (how to approach this for within Style etc.)...

Get focused MenuItem in submenu WPF

(disclaimer: not my article - I'm just a thankful user:)

Community
  • 1
  • 1
NSGaga-mostly-inactive
  • 14,052
  • 3
  • 41
  • 51
1

You can use

1) MoveCurrentToPosition(int position) see : msdn

2) MoveCurrentTo(Object item) see: msdn

3) MoveCurrentToFirst()

4) MoveCurrentToLast()

...

mlemay
  • 1,622
  • 2
  • 32
  • 53
  • 1
    Thanks. I understood this as "move the currently selected item to the specified position", not "move view to specified position". – Bent Rasmussen Mar 22 '13 at 16:29
  • Tested MoveCurrentToFirst() - doesn't work here.:/ My view is virtualized - could that be a problem? I also tried to select the item first and then call MoveCurrentToFirst(). – Bent Rasmussen Mar 22 '13 at 16:49