0

In xaml, value = {binding viewmodelValue} can be passed, but I don't know how to implement it in C#.

  • 1
    Does this answer your question? [How to set a binding in Code?](https://stackoverflow.com/questions/7525185/how-to-set-a-binding-in-code) – SomeBody Mar 11 '22 at 06:05

1 Answers1

0

You need to set the binding context to the ViewModel. In your view backend you can set

public partial class MySampleBackendPage : ContentPage
{
    public MySampleBackendPage()
    {
        InitializeComponent();

        this.BindingContext  = new ViewModel();
    }
}

This would bind your context to the view model.

Say your ViewModel class looks like this:

public class ViewModel
{
  List<string> TextBoxValues = new List<string>();
}

Now You can bind it in xaml with the following:

<ListView x:Name="EmployeeView"
            ItemsSource="{Binding TextBoxValues}">
    <ListView.ItemTemplate>
      <DataTemplate>
        <Entry Text={Binding} BindingMode="TwoWay"/>
      </DataTemplate>
    </ListView.ItemTemplate>
  </ListView>
Zee
  • 622
  • 4
  • 13