Lost 2-way binding when changes are from code-behind?

It is me again (for anyone who is familiar with my username already) with another dumb question.I just got a very quick hold of WPF Data Binding to the UI, everything is working great, except this one. Here is the situation.

  • I have a textBox, which is bind 2-way to the property of a class call "SaveTo". Here is the code in XAML

     <TextBox x:Name="SaveLocationTextBox" Margin="10,90,0,0" TextWrapping="Wrap" Grid.Column="1" Grid.Row="0" Text="{Binding SaveTo,Mode=TwoWay}" VerticalAlignment="Top" HorizontalAlignment="Stretch" FontSize="15"/> 
  • The class that contain SaveTo is implemented INotifyPropertyChanged and the SaveTo Property Setter is also correctly invoking the event.

    public string SaveTo { get { return _saveTo; } set { _saveTo = value; OnPropertyChanged("SaveTo"); } } 
  • I have a piece of code from Code-Behind of the XAML pages, which set the Text Property of the SaveLocationTextBox to something else (a string). (The piece of code is inside an event handler of another control, a button in this case)

    private void SaveLocationChooseButton_OnClick(object sender, RoutedEventArgs e) { //Some others code SaveLocationTextBox.Text = dialog.FileName + "\\"; } 

Now, when ever the Text Property of the SaveLocationTextBoxfrom the SaveLocationChooseButton_OnClick , the SaveTo Property won't get updated at all. The only way that i can force the update to happen, is to click onto the TextBox and click outside it (manually focus and out-focus it). Anybody have any suggestion? Maybe that i am missing something? I have tried to search around but can't really seem to find anything.

by AMRAAM_Missiles via /r/csharp

Leave a Reply