Xamarin ListView与xaml到C#代码中的绑定

Jon*_*Jon 4 c# xamarin xamarin.forms

如何转换以下的ListView与ItemTemplateBindingxaml文件相当于C#代码:

<ListView VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" 
    ItemsSource="{Binding A}" ItemSelected="OnClick">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextCell Text="{Binding b}" Detail="{Binding c}"/>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
Run Code Online (Sandbox Code Playgroud)

use*_*er1 8

尝试这样的事情:

ListView lv = new ListView();
lv.HorizontalOptions = LayoutOptions.FillAndExpand;
lv.VerticalOptions = LayoutOptions.FillAndExpand;
lv.SetBinding(ListView.ItemsSourceProperty, new Binding("A"));
lv.ItemSelected += (sender, args) =>
{
    onclick(sender, args);
}; //Remember to remove this event handler on dispoing of the page;
DataTemplate dt = new DataTemplate(typeof(TextCell));
dt.SetBinding(TextCell.TextProperty, new Binding("b"));
dt.SetBinding(TextCell.DetailProperty, new Binding("c"));
lv.ItemTemplate = dt;
Run Code Online (Sandbox Code Playgroud)

对于更复杂的Datatemplates做法:

DataTemplate dt = new DataTemplate(() => 
{
   var button = new Button();
   button.SetBinding(Button.TextProperty, new Binding("Name"));
   return new ViewCell { View = button };
});
Run Code Online (Sandbox Code Playgroud)