Denis
Denis

Reputation: 344

Workaround for caret textbox with binding an property changed XAML

I have a lot of textboxes binded in twoway mode ot a different properties with UpdateSourceTrigger=PropertyChanged and I have one annoying problem: when I enter or delete a value - caret moves to the begin of the string. I found a solution here: TextBox with CurrencyFormat and PropertyChanged trigger doesn't accept text right

but I cannot implement it correctly. I probably miss something.

Here is my XAMLbinding for one of textboxes:

enter code here <TextBox x:Name="TextBoxFirstPersonCameraMinPosY" Width="150" VerticalAlignment="Center" FontWeight="Normal"
                                 Text="{Binding Path=CameraLimitationParameters.FirstPersonCameraPosition.MinValue.Y, Mode=TwoWay, IsAsync=True, Delay=0, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource ToStringConverter}}" Style="{StaticResource {x:Type TextBox}}"
                                         MaxLength="40" Margin="10,0,0,0" MinWidth="150" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled" MaxWidth="150"></TextBox>

and here is my style:

enter code here<Page.Resources >
    <ObjectDataProvider x:Key="CameraMode"
        MethodName="GetValues" ObjectType="{x:Type sys:Enum}" >
        <ObjectDataProvider.MethodParameters>
            <x:Type TypeName="settingsManager:CameraBehavior"/>
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
    <settingsManager:ToStringConverter x:Key="ToStringConverter"></settingsManager:ToStringConverter>
    <Style TargetType="{x:Type TextBox}">
        <Style.Triggers>
            <Trigger Property="IsKeyboardFocusWithin"  Value="True">
                <Setter Property="Text" Value="{Binding UpdateSourceTrigger=PropertyChanged}"></Setter>
            </Trigger>
        </Style.Triggers>
    </Style>
</Page.Resources>

What did I miss an how to apply this style for all textboxes independently of binding property without duplicating code? And will this workaround work for my case? Help me please.

Upvotes: 0

Views: 642

Answers (1)

Denis
Denis

Reputation: 344

Problem was because I set IsAsync=True in TextBox. In this case Async need to be set to false.

Upvotes: 1

Related Questions