Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF TextBox.SelectAll () doesn't work

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.

like image 698
Zoliqa Avatar asked Dec 13 '22 01:12

Zoliqa


1 Answers

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.

like image 193
Zoliqa Avatar answered Dec 23 '22 04:12

Zoliqa