Matthis Kohli Matthis Kohli - 2 months ago 41x Question

WPF TextBox UpdateSourceTrigger=LostFocus keeps updating data

I am making my way through a tutorial series for WPF. Currently I am at this step

The problem I encountered is that I expect the Textbox to only update the value when I leave the TextBox. But even wether im in or outside the TextBox the value get's updated when I change the window size. That's not what I expect. Where is my error?

Here the short version of the code to test it yourself:

<Window x:Class="MainWindow"
Title="MainWindow" Height="350" Width="525">
<WrapPanel Orientation="Horizontal" Margin="0,10,0,0">
<TextBlock Text="Window dimensions: "/>
<TextBox Text="{Binding Width, UpdateSourceTrigger=LostFocus}" Width="50" />
<TextBlock Text=" x " />
<TextBox Text="{Binding Height, UpdateSourceTrigger=PropertyChanged}" Width="50"/>


But even if I'm inside the TextBox the value get's updated when I change the window size.

You yourself said that while changing the window size, the value gets updated. It's because the focus is lost from the text box. You can programmatically set focus to the textbox if you want it to be focussed even after focus is lost.

The second textbox <TextBox Text="{Binding Height, UpdateSourceTrigger=PropertyChanged}" Width="50"/> also has UpdateSourceTrigger=PropertyChanged. I hope it is not the case you're explaining, since UpdateSourceTrigger=PropertyChanged will update the source whenever a new character is entered into the textbox.