1
votes

I have a MVVM view where I bind a DataGrid.ItemsSource to an ObservableCollection in the underlying view model. The data is a live log of events that my application pulls from a server. The view model adds log entries to my ObservableCollection as they come in.

I want my DataGrid, assuming it's already scrolled all the way to the bottom, to auto scroll so that the most recently added entry is always visible. Does anyone know how to do this given the MVVM setup?

1

1 Answers

1
votes

Does anyone know how to do this given the MVVM setup?

There are really two simple options here:

  1. Use code behind. While MVVM really does discourage this, there are times when code behind is still appropriate or reasonable. Since this is a 100% pure-view related concern, using code behind (in my opinion) isn't unreasonable.
  2. Create an attached property or Blend-style behavior to add the runtime behavior you want to the View from xaml. This is still, effectively, code behind, but moves it into a reusable form, since the behavior you write can be used on any DataGrid.