mosquito87
mosquito87

Reputation: 4440

Property only set after change of focus at binded input

In my xaml I have a textbox which is binded to a property in my viewmodel:

XAML:

<TextBox HorizontalAlignment="Left" Height="30" Margin="10,10,0,0" TextWrapping="Wrap" Text="{Binding City}" VerticalAlignment="Top" Width="191" Grid.Column="1" Grid.Row="6"/>

View model:

public class MyViewModel : INotifyPropertyChanged
{
    ...
    public string City
    {
        get { return _model.Address.City; }
        set
        {
            if (_model.Address.City != value)
            {
                _model.Address.City = value;
                RaisePropertyChanged("City");
            }
        }
    }
    ...
}

Two-way-data-binding works well but it seems that a properties value is only set when the focus is changed from the city input to another object.

I would like to save my context when a hotkey (ENTER) is pressed. When I now enter a city and press ENTER afterwards, the property doesn't contain the new value. When I enter a city, change the focus to another object and press ENTER, the property contains the right value.

How to solve this problem?

Upvotes: 3

Views: 1855

Answers (1)

user3596113
user3596113

Reputation: 878

Use the UpdateTrigger and set it to PropertyChanged.

<TextBox HorizontalAlignment="Left" Height="30" Margin="10,10,0,0" TextWrapping="Wrap" Text="{Binding Country, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" VerticalAlignment="Top" Width="191" Grid.Column="1" Grid.Row="6"/>

Upvotes: 10

Related Questions