Exemplo n.º 1
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;
                }
            }
        }
Exemplo n.º 2
0
        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);
        }