Reputation: 11
myview
<Image Grid.Column="0" Grid.Row="0" Source="contactIcon.png" />
<Entry Grid.Column="1" Grid.Row="0" Text="{Binding
SelectedContact.FNAME,Mode=TwoWay}" Placeholder="First Name" />
<Entry Grid.Column="1" Grid.Row="1" Text="{Binding
SelectedContact.LNAME,Mode=TwoWay}" Placeholder="Last Name"/>
<Image Grid.Column="0" Grid.Row="2" Source="calIcon.png" />
<Entry Grid.Column="1" Grid.Row="2" Text="{Binding
SelectedContact.PHONE,Mode=TwoWay}" Placeholder="Mobile"
Keyboard="Telephone"/>
<Image Grid.Column="0" Grid.Row="3" Source="emailIcon.png" />
<Entry Grid.Column="1" Grid.Row="3" Text="{Binding
SelectedContact.EMAIL,Mode=TwoWay}" Placeholder="Email" Keyboard="Email"/>
<Entry Grid.Column="1" Grid.Row="4" Text="{Binding
SelectedContact.BALANCE,Mode=TwoWay}" Placeholder="Email"
Keyboard="Email"/>
Contact model
public int ID { get; set; }
public string FNAME { get; set; }
public string LNAME { get; set; }
public string PHONE { get; set; }
public string EMAIL { get; set; }
public Double BALANCE { get; set; }
ContactViewModel
private Contact _selectedContact;
public Contact SelectedContact{get { return _selectedContact; }set{
_selectedContact = value; OnPropertyChanged(); } }
OneWay binding is working but i want twoway binding. If i change the text or modify the text in the firstname entry then it should change the FNAME property of SelectedContact
Upvotes: 0
Views: 490
Reputation: 368
protected void RaisePropertyChanged(String property)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
protected void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression)
{
MemberExpression expression = propertyExpression.Body as MemberExpression;
RaisePropertyChanged(expression.Member.Name);
}
public event PropertyChangedEventHandler PropertyChanged;
Try this in your view for property change
RaisePropertyChanged(()=>YourPropertyName);
Upvotes: 1
Reputation: 97
You had to implement INotifyPropertyChanged which is main interface for two way binding.At your viewmodel class you had to implement like this way
public class ContactViewModel: INotifyPropertyChanged
{
private Contact _selectedContact;
public Contact SelectedContact
{
get
{ return _selectedContact; }
set
{ _selectedContact = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) {
PropertyChanged ? .Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
At your MainPage()
public MainPage() {
InitializeComponent();
vm = new ContactViewModel();
BindingContext = vm;
}
Upvotes: 0