Danw25
Danw25

Reputation: 316

WPF lostfocus not firing when pressing a button

this is the XAML code for an editable combox box for entering/selecting IP.

<ComboBox Name="ComboBoxServerIp" 
    Grid.Row="2"  Grid.Column="1"                                                            
    ItemsSource="{Binding ServerList}"          
    Text="{Binding SelectedServer, UpdateSourceTrigger=LostFocus, Mode=TwoWay}"
    IsEditable="True" IsReadOnly="False">
    <ComboBox.InputBindings>
        <KeyBinding Command="{Binding UpdateServerIpCommand}" Key="Enter"
                    CommandParameter="{Binding ElementName=ComboBoxServerIp ,Path=Text}"/>
    </ComboBox.InputBindings>   
</ComboBox>

once the user is done entering the IP it triggers a flow that takes about 10 seconds. so i used a combination of lostfocus and enter key pressed to trigger an update.

this works fine with the exception of the use case when a user presses a button without first losing focus or pressing enter. in that case the SelecterServer updat is not triggerd BEFORE THE BUTTON's COMMAND IS EXECUTED. the command reads old the SelectedServer value. not the one in the UI

tl;dr - lostfocus event not triggerd in textbox when pressing a button in the UI. how can i force an update from the UI side ?

Upvotes: 1

Views: 2435

Answers (5)

henon
henon

Reputation: 2501

I had this exact problem when doing some logic in the PreviewMouseDown handler of the Button. Putting the logic on the dispatcher solved it for me because that would allow the LostFocus event to cause a binding update before my button logic would execute.

Upvotes: 0

maori
maori

Reputation: 1

Have you implemented the INotifyPropertyChanged interface in the ViewModel? That might help solve your problem. I had a similar problem where I would enter some data into a TextBox then without clicking anywhere else I would click the button which would fire off a command but I always had the old value in the property and not the one from the UI. Once I implemented the INotifyPropertyChanged interface it worked fine.

Upvotes: 0

Alex Boutin
Alex Boutin

Reputation: 187

I had the same case, where I was using Telerik's Grid and Themes and I had the same trouble where my GridViewComboBoxColumn would not update value before the Button's click event is triggered and you had to click anywhere else before or the Data would not be saved.

In my case, I have the Opposite of DanW25's awnser and I had set my Buttons Focusable property to False to resolve something in the theme. I set the Focusable property of the Save button to True and now it all work again.

Upvotes: 0

Danw25
Danw25

Reputation: 316

found a post of the exact opposite problem, WPF Lost-Focus issue in same control.

surprisingly the opposite of that fix worked perfectly for me add this property to the button:

Focusable="False"

the lostFocus event is triggered before the button clicked event. everything works as expected.

Upvotes: 1

Dean Chalk
Dean Chalk

Reputation: 20461

Try this

Text="{Binding SelectedServer, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"

or if this is not an option, then maybe an attached property like on this answer WPF - Set Focus when a button is clicked - No Code Behind

Upvotes: 0

Related Questions