Home > Net >  How to bind in array format in mvvm type xamarin?
How to bind in array format in mvvm type xamarin?

Time:03-11

I am currently working on a xamarin project, and I am using the mvvm format.

There is a single button in the UI, and a new textbox is drawn whenever the button is pressed.

In the case of a single textbox, in xaml, you can pass the value to viewmodel.cs in the {binding text} method, but I am wondering if there is a way to pass multiple textboxes to one array of the viewmodel.

CodePudding user response:

In your Code, bind listview from a ViewModel something like this.

public class ViewModel
{
   private List<string> _TextBoxValues = new List<string>();
   public List<string> TextBoxValues
   {
     get
     { 
       return _TextBoxValues; 
     }
     set
     {
         _TextBoxValues = value;
         RaisePropertyChanged("TextBoxValues");
     }
   }
}

In the Markup XAML, bind a listview with TwoWay Binding

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

When you click on the button, call a function in the ViewModel:

public void IncrementTextBoxes()
{
   TextBoxValues.Add("");
}

This will automatically increment the textbox count, while preserving the values.

Enjoy!.

  • Related