Skip to content

Instantly share code, notes, and snippets.

@ArildF
Created July 26, 2010 21:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ArildF/491269 to your computer and use it in GitHub Desktop.
Save ArildF/491269 to your computer and use it in GitHub Desktop.
private static void CurrentlyInViewChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
var ic = (ItemsControl) dependencyObject;
var newValue = e.NewValue;
if (ic.IsLoaded)
{
BringIntoView(ic, newValue);
}
else
{
RoutedEventHandler handler = null;
handler = (sender, args) =>
{
BringIntoView(ic, newValue);
ic.Loaded -= handler;
};
ic.Loaded += handler;
}
}
private static void BringIntoView(ItemsControl ic, object newValue)
{
var presenter = (ContentPresenter) ic.ItemContainerGenerator.ContainerFromItem(newValue);
if (presenter != null)
{
presenter.BringIntoView();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment