Esempio n. 1
0
        protected override void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
        {
            base.OnItemsSourceChanged(oldValue, newValue);

            //порядок вызова важен нужно сначала центрировать а потом ставить фокус
            if (SelectedItem != null)
            {
                //скорбная песнь, для того что бы вычислить вертикальное смещение
                //что бы отобразить строку по центру используется ViewportHeight
                //но при изменение ItemsSource меняется и ViewportHeight
                //но ViewportHeight будет пересчитан только когда будет обновлен layout
                //контрола (ArrangeOverride)
                //по этому мы говорим планировщику что нужно выполнить Centrify после того как он поделает все дела
                //это приводит к неприятному эффекту "дергания" когда таблица рисуется в одном положении
                //а затем почти мгновенно в другом
                ScrollIntoView(SelectedItem);
                Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(() => {
                    DataGridHelper.Centrify(this);
                }));
            }

            //после обновление ItemsSource все визуальные элементы будут перестроены
            //и потеряют фокус
            //для того что бы восстановить фокус нужно запланировать это после того как новые элементы будут построены
            if (IsKeyboardFocusWithin)
            {
                Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(() => {
                    DataGridHelper.Focus(this);
                }));
            }
            ItemSourceChanged?.Invoke(this, new EventArgs());
        }
        public static void RedirectInput(EventPattern <TextCompositionEventArgs> message, TextBox searchText)
        {
            if (!searchText.IsEnabled)
            {
                return;
            }
            if (String.IsNullOrEmpty(message.EventArgs.Text))
            {
                return;
            }

            if (!Char.IsControl(message.EventArgs.Text[0]))
            {
                message.EventArgs.Handled = true;
                searchText.Text          += message.EventArgs.Text;
                DataGridHelper.Centrify(message.Sender as DataGrid);
            }
            else if (message.EventArgs.Text[0] == '\b')
            {
                message.EventArgs.Handled = true;
                searchText.Text           = searchText.Text.Slice(0, -2);
                DataGridHelper.Centrify(message.Sender as DataGrid);
            }
        }