using System.Collections.Specialized; using System.Windows.Controls; using Microsoft.Xaml.Behaviors; namespace SWS.Electrical { public class ListBoxScrollToBottomBehavior : Behavior { protected override void OnAttached() { base.OnAttached(); ((INotifyCollectionChanged)AssociatedObject.Items).CollectionChanged += OnCollectionChanged; } protected override void OnDetaching() { base.OnDetaching(); ((INotifyCollectionChanged)AssociatedObject.Items).CollectionChanged -= OnCollectionChanged; } private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { if (AssociatedObject.HasItems) { AssociatedObject.ScrollIntoView(AssociatedObject.Items[AssociatedObject.Items.Count - 1]); } } } }