1
votes

Has anyone successfully implemented a two-way binding on a TextView with MVVM Light? Two-way works perfectly fine with EditView, but the moment I try two-way binding with TextView - only one way binding works. Does anyone have any insight as to why, please?

View Model:

private string _someField;
public string SomeField
{
    get { return _someField; }
    set { Set(ref _someField, value); }
}

View:

private EditText _editableText;
public EditText EditableText;
{
  get { return _editableTex ?? (_editableTex = FindViewById<EditText>(Resource.Id.editText1)); }
}

private TextView _simpleText
public TextView SimpleText
{
  get { return _simpleText ?? (_simpleText = FindViewById<TextView>(Resource.Id.textDateDisplay)); }
}

protected override void OnCreate(Bundle savedInstanceState)
{
    bindings.Add(this.SetBinding(() => vm.SomeField, () => EditableText.Text, BindingMode.TwoWay)); 
    bindings.Add(this.SetBinding(() => vm.SomeField, () => SimpleText.Text, BindingMode.TwoWay));
}

No errors are thrown. But when I change (in code) of the View the text of the EditableText (EditableText.Text="asdf";) the corresponding set { Set(ref _someField, value); } triggers in the VewModel. It also triggers, naturally, if I just type in the EditText widget.

However, when I change (in code) the text property of the SimpleText (SimpleText.Text="2145";) it does not trigger the corresponding set.

Does anyone know why?

Thank you very much for help, mike

1

1 Answers

0
votes

You can't set a two-way binding with TextViews, because they doesn't allow input by user.

When you are using MVVM pattern you should never update the view. You should update the bound model property. So you should replace SimpleText.Text = "2145"; with vm.SomeField = "2145";.