0

I have xaml file:

<TextBox Text="{Binding Student.SName, Mode=TwoWay}"/>
<TextBox Text="{Binding Student.Name, Mode=TwoWay}"/>

And my Definition to this xaml file:

public Model.Student Student { get; set; }

And the Student Class which is in the property:

public class Student : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
       if (PropertyChanged != null)
       {
           PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
       }
    }

    private string _SName;
    private string _Name;

    public string SName
    {
        get { return _SName; }
        set
        {
            if (_SName != value)
            {
                _SName = value;
                OnPropertyChanged("SName");
            }
        }
    }

    public string Name
    {
        get { return _Name; }
        set
        {
            if (_Name != value)
            {
               _Name = value;
               OnPropertyChanged("Name");
            }
        }
    }
}

What can I do, to change the value in the Student-Property automatically when entering text?

Yael
  • 1,566
  • 3
  • 18
  • 25
goodniko
  • 163
  • 3
  • 14
  • @EhsanSajjad When i add info in .cs file View Update, but When i write text in TextBox it's doesn't update for me;( – goodniko Feb 18 '16 at 17:59
  • Show us how do you set the DataContext. – Peter Feb 18 '16 at 18:06
  • 1
    You can set "UpdateSourceTrigger " to "Lost focus" – Sudipta Kumar Sahoo Feb 18 '16 at 18:08
  • @Peter i open windows as View.AddStudent add = new View.AddStudent(); add.DataContext = new AddStudentViewModel(); add.ShowDialog(); – goodniko Feb 18 '16 at 18:12
  • 2
    Set the `UpdateSourceTrigger` property of the Bindings to `PropertyChanged`. It is `LostFocus` by default, which will trigger an update of the binding source (i.e. the view model) property only when the TextBox loses focus. – Clemens Feb 18 '16 at 18:22
  • @Clemens thx so match but can i give me some sample for this? – goodniko Feb 18 '16 at 18:44
  • 2
    `Text="{Binding Student.Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"` – Clemens Feb 18 '16 at 18:55
  • @Clemens alas, it's dont work for me ;( – goodniko Feb 18 '16 at 19:06
  • 1
    Just to clarify, you have an `AddStudentViewModel` class which contains a property for `Student`? And this is the property you are saying is not updating? Because in your question you say the definition of the `Student` property is in the XAML, which makes me think your DataContext is not setup correctly, and you are probably looking at two separate instances of the `Student` object. – Rachel Feb 18 '16 at 19:27
  • @Rachel i write public Model.Student Student { get; set; } = new Model.Student(); and it's work! ;) Thanks!) – goodniko Feb 18 '16 at 19:50
  • @user3588235 Be sure you understand what the DataContext is used for if you're working with WPF :) Here's one SO answer I wrote explaining it which you may find useful : http://stackoverflow.com/a/7262322/302677 – Rachel Feb 18 '16 at 19:58

0 Answers0