Exemplo n.º 1
0
        public static void OnAutoScrollToEndChanged(DependencyObject s, DependencyPropertyChangedEventArgs e)
        {
            var itemsControl = s as ItemsControl;

            if (itemsControl != null)
            {
                var itemsControlItems = itemsControl.Items;
                var data = itemsControlItems.SourceCollection as INotifyCollectionChanged;

                var scrollToEndHandler = new NotifyCollectionChangedEventHandler(
                    (s1, e1) =>
                {
                    if (itemsControl.Items.Count > 0)
                    {
                        ExecuteOnUIThread.Invoke(() =>
                        {
                            ScrollViewer scrollViewer = VisualTree.GetDescendantByType <ScrollViewer>(itemsControl);
                            scrollViewer?.ScrollToEnd();
                        });
                    }
                });

                if (data != null)
                {
                    if ((bool)e.NewValue)
                    {
                        data.CollectionChanged += scrollToEndHandler;
                    }
                    else
                    {
                        data.CollectionChanged -= scrollToEndHandler;
                    }
                }
            }
        }
Exemplo n.º 2
0
        public static void OnAutoScrollToEndChanged(DependencyObject s, DependencyPropertyChangedEventArgs e)
        {
            var listBox = s as ListBox;

            if (listBox != null)
            {
                var listBoxItems = listBox.Items;
                var data         = listBoxItems.SourceCollection as INotifyCollectionChanged;

                var scrollToEndHandler = new NotifyCollectionChangedEventHandler(
                    (s1, e1) =>
                {
                    if (listBox.Items.Count > 0)
                    {
                        //object lastItem = listBox.Items[listBox.Items.Count - 1];
                        //listBoxItems.MoveCurrentTo(lastItem);
                        //listBox.ScrollIntoView(lastItem);
                        ExecuteOnUIThread.Invoke(() =>
                        {
                            ScrollViewer scrollViewer = VisualTree.GetDescendantByType <ScrollViewer>(listBox);
                            scrollViewer?.ScrollToEnd();
                        });
                    }
                });

                if (data != null)
                {
                    if ((bool)e.NewValue)
                    {
                        data.CollectionChanged += scrollToEndHandler;
                    }
                    else
                    {
                        data.CollectionChanged -= scrollToEndHandler;
                    }
                }
            }
        }