1

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 ?

Danw25
  • 306
  • 2
  • 13
  • I can't reproduce the issue. If I click a button in UI, the value was entered in combo box being set to the bound property. Put a breakpoint in `SelectedServer` setter. – Rekshino Jan 03 '19 at 09:05
  • @Rekshino added a clarification.the button reads the value from SelectedServer – Danw25 Jan 03 '19 at 10:38
  • Hmm.. On my side the setter of property bound with `Text` being hitted first, before command is executed. I have a usual binding for the button: `Command="{Binding ...}"` – Rekshino Jan 03 '19 at 10:54
  • Why do you need to set the property when the focus is lost in the first place? Your source property should not trigger any flow. A command should. – mm8 Jan 03 '19 at 16:19

4 Answers4

1

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.

Danw25
  • 306
  • 2
  • 13
0

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

Dean Chalk
  • 20,076
  • 6
  • 59
  • 90
0

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.

Alex Boutin
  • 157
  • 1
  • 8
0

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.

BlackBox
  • 2,223
  • 1
  • 21
  • 37
maori
  • 1