Problem with Binding and Caretindex

Asked

Viewed 21 times

1

I have a Datagrid List:

<DataGrid Name="GridLista" 
                     Grid.ColumnSpan="20" 
                     Margin="5" 
                     Grid.RowSpan="17" 
                     Grid.Row="3"
                     CanUserAddRows="False"
                     CanUserReorderColumns="True"
                     AutoGenerateColumns="False" 
                     SelectionUnit="FullRow"
                     SelectionMode="Extended"
                     ItemsSource="{Binding Path=Lista}"
                     SelectedValue="{Binding Path=Model, Mode = TwoWay}"
                     IsReadOnly="True"
              >
                    <DataGrid.Columns>
                        <DataGridTextColumn Width="0.1*" Binding="{Binding Codigo}" Header="Código"/>
                        <DataGridTextColumn Width="0.9*" Binding="{Binding Nome}" Header="Nome Operador"/>
                    </DataGrid.Columns>
                </DataGrid>

After clicking on one of the lines, all the textboxes are filled, below one of them:

<TextBox Name="textOperadorNome" Text = "{Binding Model.Nome, Mode = TwoWay, UpdateSourceTrigger=PropertyChanged}" CharacterCasing="Upper" Margin="5" Grid.ColumnSpan="8"  Grid.Row="2" Padding="0" VerticalContentAlignment="Center" Grid.Column="4"/>

So far no problem, everything working.

My problem is: whenever it is filled after being clicked on the table, the textbox gets the focus and instead of it placing the cursor at the end of the text, it puts at the beginning, there is some way to fix this problem?

1 answer

0

You can solve this question in two ways.


Solution A

You can add a default value to the property CaretIndex, as was done below.

<TextBox Name="textOperadorNome" Text = "{Binding Model.Nome, Mode = TwoWay, UpdateSourceTrigger=PropertyChanged}" CaretIndex="1000" CharacterCasing="Upper" Margin="5" Grid.ColumnSpan="8"  Grid.Row="2" Padding="0" VerticalContentAlignment="Center" Grid.Column="4"/>

For this code one must observe three details.

1 - The estate CaretIndex should be defined after the definition of the value of the Text, otherwise the solution will not work.

2 - The value applied to CaretIndex should be high because it sets the final position of your cursor and if it is less than the number of characters in your TextBox, the cursor will not be at the end of the text as desired.

3 - In case the user clicks TextBox and place the cursor in any position other than the end, most likely by clicking the grid to make a new upload on TextBox, the cursor will not end, because the previous action changed the CaretIntex of TextBox in the process, no longer defined initially.


Solution B

You can add an action to the event GotFocus, in order to execute a function that will define your CaretIndex value of your text size.

Below is the code of TextBox with the event:

<TextBox Name="textOperadorNome" Text = "{Binding Model.Nome, Mode = TwoWay, UpdateSourceTrigger=PropertyChanged}" GotFocus="TextOperadorNome_GotFocus" CharacterCasing="Upper" Margin="5" Grid.ColumnSpan="8"  Grid.Row="2" Padding="0" VerticalContentAlignment="Center" Grid.Column="4"/>

Below is the definition of the execution of the Code Behind:

private void TextOperadorNome_GotFocus(object sender, RoutedEventArgs e)
{
    textOperadorNome.CaretIndex = textOperadorNome.Text.Length;
}

Observing: The B solution is more reliable and will ensure that you always get the desired result!

If you want to check, there is another question in the community about this subject on the link: Set the Caret cursor position to the end.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.