简体   繁体   English

在WPF中,无法选择TextBox的所有内容

[英]Selection of all of the content of TextBox is not working as expected in WPF

Im having troubles with Selecting All content inside of a TextBox. 我在Selecting All文本框内的Selecting All内容时遇到麻烦。

Ussually by pressing enter I'm jumping from one textbox to another, because there are like 6-7 TextBoxes below each other in my Grid, and by pressing enter I need to jump from one to another, 通常,按Enter键,我会从一个文本框跳到另一个文本框,因为在我的网格中彼此之间有6-7个文本框,然后按Enter键,我需要从一个文本框跳至另一个文本框,

 private void Grid_PreviewKeyDown_1(object sender, KeyEventArgs e)
  {

     if (e.Key == Key.Enter)
     {

        UIElement element = e.Source as UIElement;
        element.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));

        //TextBox tb = (sender as TextBox);
        //if (tb != null)
        //{
        //    tb.SelectAll();
        //}

     }
  }

And while I'm on some of them when I press Enter I'm doing some calculation like this: 当我按Enter进入其中一些时,我正在进行如下计算:

private void txt2_PreviewKeyDown(object sender, KeyEventArgs e)
{
   if (e.Key == Key.Return)
        {
            try
            {
                CalculateSomethingFromOtherTextBoxes();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
}

My Question is next: When I jump from each other and when I finish calculation (enter is pressed), the next TextBox I will jump to I would like SELECTALL of TextBox's content when I jumped on it. 我的下一个问题是:当我彼此跳转并完成计算(按Enter键)时,我将跳至下一个TextBox,当我跳到下一个TextBox时,我希望对它进行全选。 In case I want to edit some value or whatever, and it is confusing sometimes content insidee is selected and sometimes it is not. 万一我想编辑某个值或其他值,这会造成混乱,有时选择内容内部,有时则不然。

I tried setting GotFocus event on each of TextBoxes and It looks like this: 我尝试在每个TextBoxes上设置GotFocus事件,它看起来像这样:

private void txt3_GotFocus(object sender, RoutedEventArgs e)
{
     txt3.SelectAll();
}

But unfortunately somehow this is sometimes working sometimes it is not, I mean all of content is selected sometimes and sometimes it is not.. 但是不幸的是,有时这有时是可行的,有时却没有,我的意思是有时选择了所有内容,有时却没有。

Thanks guys Cheers 谢谢大家的欢呼

Try to handle the GotKeyboardFocus event instead of the GotFocus event. 尝试处理GotKeyboardFocus事件而不是GotFocus事件。 This should work: 这应该工作:

private void txt3_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
    txt3.SelectAll();
}

There is no property that you can set to select all of the Text in a TextBlock or TextBox. 没有可以设置为选择TextBlock或TextBox中的所有Text的属性。 Selecting all text must be accomplished using the TextBoxBase.SelectAll Method. 选择所有文本必须使用TextBoxBase.SelectAll方法来完成。 What you could do in a Style is to set an event handler for the GotFocus event, where the handler code would call SelectAll, but your handler would of course need to be in code and not XAML. 您可以在样式中执行的操作是为GotFocus事件设置事件处理程序,该处理程序代码将调用SelectAll,但是您的处理程序当然需要在代码中,而不是XAML。

One other possibility would be for you to create an Attached Property that would select the text whenever the TextBox gets focus, but again it's not possible to do this in XAML. 另一种可能性是让您创建一个附加属性,该属性将在TextBox获得焦点时选择文本,但是同样在XAML中不可能这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM