繁体   English   中英

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

[英]Xamarin ListView with Binding in xaml to C# code

如何将以下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>

尝试这样的事情:

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;

对于更复杂的Datatemplates做:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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