【问题标题】:How can I select (with focus) the text in telerik:RadNumericUpDown如何选择(重点)telerik 中的文本:RadNumericUpDown
【发布时间】:2014-09-30 20:46:49
【问题描述】:

这个 NumericUpDown (NUD) 漂浮在地图上。当它变得可见时,我需要重定向控件内的下一个击键来覆盖当前值。

我非常痛苦地找到了这个解决方案:

    private void LengthInput_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        if ((bool)(e.NewValue))
        {
...
            LengthInputBox.ShowButtons = true;
            try
            {
                LengthInputBox.Focus();
                if (m_lengthTextBox == null)
                {
                    LengthInputBox.ApplyTemplate();
                    m_lengthTextBox = LengthInputBox.Template.FindName("textbox", LengthInputBox) as TextBox;
                }
                if (m_lengthTextBox != null)
                {
                    m_lengthTextBox.SelectAll();
                    m_lengthTextBox.Focus();
                }
            }
            finally
            {
                LengthInputBox.ShowButtons = false;
            }
...

NUD 是 LengthInputBox 控件。 Focus 方法将焦点设置在 NUD 按钮上。

Template.FindName("textbox"... 检索 NUD 的内部 TextBox。如果找到,或者以前找到,则选择全部并设置焦点在文本上。

最后,我删除了向上/向下按钮(我不需要它们。尽管我已经做了很多有或没有它们的变化,但它们的存在并不会改变行为......)

第一次成功,第二次又失败了。

有什么想法吗?

【问题讨论】:

    标签: wpf telerik


    【解决方案1】:

    选择和聚焦有点慢。使用 Dispatcher 解决了这个问题:

        private void LengthInputBox_GotFocus(object sender, RoutedEventArgs e)
        {
            if (m_lengthTextBox == null)
            {
                LengthInputBox.ApplyTemplate();
                m_lengthTextBox = LengthInputBox.Template.FindName("textbox", LengthInputBox) as TextBox;
            }
            if (m_lengthTextBox != null)
            {
                m_lengthTextBox.Focusable = true;
                m_lengthTextBox.IsTabStop = true;
    
                if (!m_lengthTextBox.IsFocused)
                    Dispatcher.BeginInvoke(new Action(() =>
                    {
                        var dot = m_lengthTextBox.Text.IndexOf('.');
                        m_lengthTextBox.Select(dot, m_lengthTextBox.Text.Length - dot);
                        m_lengthTextBox.Focus();
                    }));
            }
            LengthInputBox.CaptureMouse();
        }
    

    (别忘了松开鼠标:

        private void LengthInput_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Escape:
                case Key.Enter:
                    LengthInputBox.ReleaseMouseCapture();
                    ViewModel.IsLengthInputVisible = false;
                    e.Handled = true;
                    break;
            }
        }
    

    )

    【讨论】:

      猜你喜欢
      • 2020-05-14
      • 1970-01-01
      • 1970-01-01
      • 2012-01-13
      • 2018-12-02
      • 2010-09-22
      • 2021-11-25
      相关资源
      最近更新 更多