Created
October 10, 2014 10:28
-
-
Save cmorgado/8741d2fb2ae42070cef0 to your computer and use it in GitHub Desktop.
Behavior Scroll to the last GridviewItem when you change the ItemsSource
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ScrollToLeftGridViewBehavior : DependencyObject, IBehavior | |
{ | |
public DependencyObject AssociatedObject { get; private set; } | |
public object ItemsSource | |
{ | |
get { return (object)GetValue(ItemsSourceProperty); } | |
set { SetValue(ItemsSourceProperty, value); } | |
} | |
public static readonly DependencyProperty ItemsSourceProperty = | |
DependencyProperty.Register("ItemsSource", typeof(object), | |
typeof(ScrollToLeftGridViewBehavior), | |
new PropertyMetadata(null, ItemsSourcePropertyChanged)); | |
private static void ItemsSourcePropertyChanged(object sender, | |
DependencyPropertyChangedEventArgs e) | |
{ | |
var behavior = sender as ScrollToLeftGridViewBehavior; | |
if (behavior.AssociatedObject == null || e.NewValue == null) return; | |
var collection = behavior.ItemsSource as INotifyCollectionChanged; | |
if (collection != null) | |
{ | |
collection.CollectionChanged += (s, args) => | |
{ | |
var scrollViewer = behavior.AssociatedObject | |
.GetFirstDescendantOfType<ScrollViewer>(); | |
scrollViewer.ChangeView( scrollViewer.ActualWidth> scrollViewer.ExtentWidth ? 0 :scrollViewer.ExtentWidth-60, null, null); | |
}; | |
} | |
} | |
public void Attach(DependencyObject associatedObject) | |
{ | |
var control = associatedObject as GridView; | |
if (control == null) | |
throw new ArgumentException( | |
"ScrollToBottomGridViewBehavior can be attached only to GridViewView."); | |
AssociatedObject = associatedObject; | |
} | |
public void Detach() | |
{ | |
AssociatedObject = null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment