I am using the MVVM pattern, I have a view which creates a new ViewModel
, after the user clicks save, this view is closed and a seperate view is opened which displays a collection of view models in a ListView
.
This ListView
is sorted in alphabetical order, so the new ViewModel
may appear at the bottom of the ListBox
, which is not immediately visible to the user.
My question is how do I get the view to auto scroll to the newly added item?
I guess that It will be using attached behaviours, and the ScrollIntoView
event on the ListView
, however its which event that I need to capture from the GridView
that I am unsure of..
Cheers
Another solution using
ListBox
. To implement auto-scrolling, you can create a custom control!C#
XAML
Here is how you use the control in XAML:
Somewhere you need to specify how you access this control. This completely depends on your project setup.
How it Works
To create a custom control you should only need C# code. We do this by extending a
ListBox
and only add a single property, AutoScroll. Because it is a dependency property, it will partake in the WPF binding system, which also makes it available in the Visual Studio designer.Covering dependency properties is a rather large topic, but is integral to create custom controls. You can learn more on the Control Authoring Overview or the Dependency Properties Overview.
The objective is to subscribe to the collection changed event of the underlying item collection so we can respond by scrolling to the bottom whenever a new item is added. We must subscribe to this event at two places.
AutoScroll
is set to true, we need to subscribe. The value ofAutoScroll
may change at any time and we should be able to respond accordingly. If set to false, we should instruct the control to stop scrolling to the bottom by unsubscribing.AutoScroll
needs to only be set at compile-time, we need a method of subscribing upon startup. This is done by using the control's constructor.Why Create a Custom Control
First and foremost, we have simplified the XAML as far as reasonably possible. We only need to access the control and optionally specify or bind to the
AutoScroll
property.It is MVVM compliant. Our view-model doesn't need to worry about the
AutoScroll
functionality because it is self-contained in the control. At the same time, the view model can provide a property which theAutoScroll
property is bound to, giving us the desired decoupling of the view & view-model.Additionally, we have avoided the use of behaviors. This means we have removed two dependencies from our project (granted this was the only reason those dependencies were included in the first place). We can safely omit System.Windows.Interactivity and Microsoft.Expressions.Interactions from the project references.
Drawbacks
There is only one drawback to this approach. The underlying items collection must implement
INotifyCollectionChanged
. In most cases, this is a non-issue. If you are using MVVM, you probably already have your items wrapped up inside anObservableCollection
, which already implements our required interface.Enjoy! :-)
This solution is for a ListBox, but it could be modified for a ListView... This will scroll the selected item into view when you change the selected item from the ViewModel.
Class:
Add the xmlns to your view:
Add the style to the resources of the Window/UserControl:
Implement the listbox:
hmm talk about overkill, for a more simple approach and the one i imagine most will use....
for listview simply whack in:
And for Listbox simply whack in:
To your listview item add (..etc) method... .. or whack it on a timer tick.
The above way under OP seems to much to do for me i am lazy... All code explains its self.
Add a selected item DependecyProperty to the class which contains the collection. Bind the SelectedItem of the listview to it. After adding the new model to the collection set the selected item DependencyProperty.
This might not apply for WPF, but in WinForms the code is similar to
lstData.EnsureVisible(itemIndex);