views:

617

answers:

2

Hi!

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.

A: 

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).

Jeremy
Unfortunatly, your solution doesn't work either. Nothing happens when textBox.SelectAll () is called.
Zoliq
A: 

Okay, if anyone is interested, the solution to this problem of mine is 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 probabily the SelectAll () and Focus () calls are ignored without calling the e.Handled = true; statement.

Hope it'll help someone.

Zoliq