简体   繁体   中英

How to Binding via C# code, not Binding via xaml?

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM