WPF TextBox.SelectAll () doesn't work

Zoliqa picture Zoliqa · Oct 23, 2009 · Viewed 9.9k times · Source

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.

Answer

Zoliqa picture Zoliqa · Feb 23, 2010

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.