IMHO, the clearest way to do this is to use Behavior through AttachedProperty . AttachedProperty is a mechanism for expanding existing management capabilities.
First create an AtachedProperty storage AtachedProperty , for example:
public class ScrollViewerBehavior { public static bool GetAutoScrollToTop(DependencyObject obj) { return (bool)obj.GetValue(AutoScrollToTopProperty); } public static void SetAutoScrollToTop(DependencyObject obj, bool value) { obj.SetValue(AutoScrollToTopProperty, value); } public static readonly DependencyProperty AutoScrollToTopProperty = DependencyProperty.RegisterAttached("AutoScrollToTop", typeof(bool), typeof(ScrollViewerBehavior), new PropertyMetadata(false, (o, e) => { var scrollViewer = o as ScrollViewer; if (scrollViewer == null) { return; } if ((bool)e.NewValue) { scrollViewer.ScrollToTop(); SetAutoScrollToTop(o, false); } })); }
This attached property allows ScrollViewer have a “magically” new property of type Boolean , acting as a DependencyProperty in your XAML. If you bind this property to a standard property in the ViewModel, for example:
private bool _reset; public bool Reset { get { return _reset; } set { _reset = value; if(PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Reset")); } }
(again, the name is up to you) and then you set this Reset property to true , your ScrollViewer will scroll up. I named AtachedProperty as AutoScrollToTop, but this name is not important for this purpose.
XAML will look something like this:
<ScrollViewer my:ScrollViewerBehavior.AutoScrollToTop="{Binding Reset, Mode=TwoWay}"> <ListView> <ListView.View> <GridView> <GridViewColumn Header = "Name" DisplayMemberBinding="{Binding Path=Name}" /> </GridView> </ListView.View> </ListView> </ScrollViewer>
Note: my is the namespace in which your ScrollViewerBehavior class ScrollViewerBehavior . For example: xmlns:my="clr-namespace:MyApp.Behaviors"
Finally, the only thing you need to do in your ViewModel is to set Reset = true when you like, in your case when you add or remove an item from the collection.