WPF TextBox.SelectAll () doesn't work
Asked Answered
C

3

5

I have used the following template in my project:

<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 is able to navigate from cell to cell within the matrix and I would like to highlight the data in the selected textbox but it doesn't work. I called TextBox.Focus () and TextBox.SelectAll () to achieve the effect but nothing. The Focus () works but the text never gets highlighted.

Any help is welcome and appreciated.

Cavit answered 23/10, 2009 at 12:28 Comment(0)
C
15

Okay, if anyone is interested, the solution to this problem of mine was to include the statement e.Handled = true; in the event handler method where the textBox.SelectAll() and textBox.Focus() are called.

The problem was that I attached an event handler to the textbox's PreviewKeyDown event which handles a tunneling event and probably the SelectAll() and Focus() calls are ignored without calling the e.Handled = true; statement.

Hope it'll help someone.

Cavit answered 23/2, 2010 at 12:43 Comment(0)
S
0

Without the rest of your code it's difficult to say whether this will work for you, but I put together a small sample using your DataTemplate (minus the parts that refer to code that wasn't posted).

I was able to select all the text in the text boxes by adding a GotFocus event handler to the TextBox in the DataTemplate:

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

And the code-behind:

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

Let me know if you are attempting to select all under different circumstances (not when the box receives focus).

Shammy answered 3/12, 2009 at 18:55 Comment(1)
Unfortunatly, your solution doesn't work either. Nothing happens when textBox.SelectAll () is called.Cavit
J
-2

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

Justinejustinian answered 2/5, 2013 at 0:23 Comment(1)
Hi, if you could add a brief summary of the solution, this will make your answer much more useful, plus it will still have value if the linked article is ever lost.Engracia

© 2022 - 2024 — McMap. All rights reserved.