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;
                    }
                }
            }
        }
Exemple #2
0
        public static void OnAutoScrollToSelectedItemChanged(DependencyObject s, DependencyPropertyChangedEventArgs e)
        {
            var listBox = s as ListBox;

            if (listBox != null)
            {
                var scrollToSelectionHandler = new SelectionChangedEventHandler(
                    (sender, args) => ExecuteOnUIThread.InvokeAsync(() =>
                {
                    //listBox.UpdateLayout();
                    listBox.Focus();         // set focus to display selected item in blue instead of gray because list is not focused
                    if (listBox.SelectedItem != null)
                    {
                        listBox.ScrollIntoView(listBox.SelectedItem);
                    }
                }, DispatcherPriority.ContextIdle));

                if ((bool)e.NewValue)
                {
                    listBox.SelectionChanged += scrollToSelectionHandler;
                }
                else
                {
                    listBox.SelectionChanged -= scrollToSelectionHandler;
                }
            }
        }
        private static object OnIsFocusedPropertyChanged(DependencyObject d, object value)
        {
            var uie = (UIElement)d;

            if ((bool)value)
            {
                //uie.Focus(); // Don't care about false values.
                //Keyboard.Focus(uie);
                //FocusManager.SetFocusedElement(d, uie);
                ExecuteOnUIThread.InvokeAsync(() => Keyboard.Focus(uie), DispatcherPriority.ContextIdle);
            }
            return(value);
        }
Exemple #4
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;
                    }
                }
            }
        }