WPF TextBox.SelectAll () not working

In my project, I used the following template:

<DataTemplate 
    x:Key="textBoxDataTemplate">
    <TextBox 
        Name="textBox"
        ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"
        Tag="{Binding}"
        PreviewKeyDown="cellValueTextBoxKeyDown">
        <TextBox.Text>
            <MultiBinding
                Converter="{StaticResource intToStringMultiConverter}">
                <Binding 
                    Path="CellValue"
                    Mode="TwoWay">
                        <Binding.ValidationRules>
                            <y:MatrixCellValueRule 
                                MaxValue="200" />
                        </Binding.ValidationRules>
                </Binding>
                <Binding 
                    RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type y:MatrixGrid}}" 
                    Path="Tag"
                    Mode="OneWay" />
            </MultiBinding>
        </TextBox.Text>
    </TextBox>
</DataTemplate>

I used this template to create an editable matrix for the user. The user can move from cell to cell inside the matrix, and I would like to highlight the data in the selected text box, but this will not work. I called TextBox.Focus () and TextBox.SelectAll () to achieve the effect, but nothing. Focus () works, but text never stands out.

Any help is appreciated and appreciated.

+3
source share
3 answers

, - , , e.Handled = true; , textBox.SelectAll() textBox.Focus().

, textbox PreviewKeyDown, , , , SelectAll() Focus() e.Handled = true;.

, -.

+12

, , DataTemplate ( , , ).

, GotFocus TextBox DataTemplate:

<TextBox 
    ...
    GotFocus="textBox_GotFocus"
    ...>
...
</TextBox>

:

    private void textBox_GotFocus(object sender, RoutedEventArgs e)
    {
        TextBox textBox = sender as TextBox;
        if (textBox != null)
        {
            textBox.SelectAll();
        }
    }

, ( , ).

0

Here's a very good very simple solution (I don’t know if it works for your template, but give it a try): http://social.msdn.microsoft.com/forums/en-US/wpf/thread/564b5731-af8a- 49bf-b297-6d179615819f

-1
source

Source: https://habr.com/ru/post/1720890/


All Articles