.NET/C# First Visible Item (ListBox) Attached Behavior

I improved and refactored my previous code to get and set a ListBox’s first visible item. It is now realized as attached behavior and supports two-way binding:

ListBoxExtensions

ListBoxExtensions adds some methods to the ListBox class to get and set its first visible item. It expects the ListBox to use a VirtualizingStackPanel to hold its items which provides access to scroll offsets directly. ListBoxExtensions checks the panel’s orientation (horizontal or vertical) and gets/sets the respective offset automatically.

public static class ListBoxExtension
{
  public static void SetFirstVisibleItem(this ListBox listBox, object item)
  {
    PerformScroll(listBox, item);
  }
  public static object GetFirstVisibleItem(this ListBox listBox)
  {
    return listBox.Items.Count > 0 ?
      listBox.Items[listBox.GetPanelOffset()] : null;
  }
  // [...]
}

ExposeFirstVisibleItemBehavior

ExposeFirstVisibleItemBehavior realizes an attached behavior for ListBoxes to enable binding to its first visible item. It listens to scroll changed events to update the first visible item.

public static readonly DependencyProperty FirstVisibleItemProperty =
  DependencyProperty.RegisterAttached("FirstVisibleItem", typeof(object), typeof(
    new FrameworkPropertyMetadata() { 
      PropertyChangedCallback = OnFirstVisibleItemChanged, 
      BindsTwoWayByDefault = true });

It can be used as follows for example (to show the first visible item in a TextBox):

<ListBox Name="VerticalListBox" ItemsSource="{Binding Collection}"               
  helpers:ExposeFirstVisibleItemBehavior.FirstVisibleItem="{Binding ElementName=VerticalListBoxFirstVisibleItem, Path=Text}"/>

Source Code Download

ExposeFirstVisibleItemBehavior VisualStudio project