2013-03-22 68 views
0

的ListCollectionView都有一個屬性獲取當前位置(CurrentPosition):ListCollectionView.CurrentPosition二傳手

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

但該屬性爲只讀。

獲取(視情況經過排序和過濾的)視圖中CurrentItem的序號位置。

那麼怎麼能設置的當前位置呢?我希望這不涉及行爲...

我需要滾動到一個ListView或DataGrid的頂部,在我看來,應該可以輕鬆設置一個屬性,如那個實現它,沒有進入視野的膽量。

在此先感謝。

更新:

這是目前我自己的解決方法 - 但它是一種disguisting - 當一個人想要使用MVVM(使用F#):

let linesControl = context.Window.FindName("ObjectListView") :?> ListView 
let scrollBorder = VisualTreeHelper.GetChild(linesControl, 0) :?> Border 
let scrollViewer = scrollBorder.Child :?> ScrollViewer 
scrollViewer.ScrollToVerticalOffset(0.0) 
+0

兩個DataGrid和ListView中有一個名爲'ScrollIntoView'方法。你必須傳遞你想要滾動的元素。 'grid.ScrollIntoView(grid.Items [0])'可以在你的情況下工作,我猜? – 2013-03-22 17:43:13

回答

1

您可以使用

1)MoveCurrentToPosition(int position) 看到:msdn

2)MoveCurrentTo(Object item) 看到:msdn

3)MoveCurrentToFirst()

4)MoveCurrentToLast()

...

+1

謝謝。我將其理解爲「將當前選定的項目移動到指定的位置」,而不是「將視圖移動到指定的位置」。 – 2013-03-22 16:29:16

+0

Tested MoveCurrentToFirst() - 在這裏不起作用.:/我的視圖被虛擬化 - 這可能是一個問題嗎?我也嘗試先選擇該項目,然後調用MoveCurrentToFirst()。 – 2013-03-22 16:49:52

+0

MoveCurrentToPosition(0)不起作用? – mlemay 2013-03-22 17:09:13

2

我用的基本上是attached property解決只讀,非綁定的屬性(即使大部分時間你不需要寫,綁定失敗)..

本文解決類似問題ActualWidth/ActualHeight ...

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

...你可以下載代碼/例如,有(或http://dl.dropbox.com/u/39657172/Blog/PushBindingInStyleDemo.zip)。

你所使用的綁定是一樣的東西......

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

我並沒有真正嘗試在你的榜樣 - 但它的樣子 - 這應該工作以同樣的方式,如果有CurrentPosition已經。


另外,如果需要更多細節,請看看這篇早期文章(如何在Style等內部實現)...

Get focused MenuItem in submenu WPF

(免責聲明:沒有我的文章 - 我只是一顆感恩的用戶:)