Search code examples
c#wpfmvvmlost-focusupdatesourcetrigger

Button needs to be clicked twice due to textbox lost focus


I have a textbox which has UpdateSourceTrigger = LostFocus.

When I type something in textbox and immediately click on button, the button click event does not fire. I think textbox simply loses the focus to button. When i click on button again it fires click event.

How to overcome this issue?

Here is my XAML code:

<TextBox x:Name="ApText"
                         Width="135"
                         Margin="0,4,0,0"
                         HorizontalAlignment="Left"
                         VerticalAlignment="Top"
                         Text="{Binding Value, Mode=TwoWay, NotifyOnValidationError=True, UpdateSourceTrigger=LostFocus, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}"
                         TextWrapping="Wrap"
                         Validation.ErrorTemplate="{DynamicResource UI.ErrorTemplateStyle}" />

<Button
                    Width="100"
                    MinWidth="60"
                    MinHeight="40"
                    Margin="0,8,0,0"
                    HorizontalAlignment="Left"
                    VerticalAlignment="Bottom"
                    Command="{Binding SaveCommand}"
                    Content="Save" />

Solution

  • Found a solution for this. Just changed Clickmode of Button to Press. And voila its working.

    Updated Code:

    <Button
                        Width="100"
                        MinWidth="60"
                        MinHeight="40"
                        Margin="0,8,0,0"
                        ClickMode="Press"
                        HorizontalAlignment="Left"
                        VerticalAlignment="Bottom"
                        Command="{Binding SaveCommand}"
                        Content="Save" />