android - Xamarin 获取动态 ListView DataTemplate 内的按钮单击事件数据

标签 android listview xamarin datatemplate

我试图知道在 DataTemplate 内带有自定义按钮的动态 ListView 中选择/单击了哪个项目,并尝试一些可能性。 我找到了一种方法来了解已单击哪个项目,创建一个标签并向其添加值以识别已选择哪个项目,但我认为这不是更好的方法。

这是我的代码的一部分,知道如何做得更好吗?

var listView = new ListView();
listView.ItemsSource = await db.GetNodeMCUsAsync();
listView.ItemTemplate = new DataTemplate(() =>
{
    Label llb_id = new Label();
    llb_id.SetBinding(Label.TextProperty, "Id");

    Label lbl_binding = new Label()
    {
        VerticalOptions = LayoutOptions.Center,
        TextColor = Color.Black,
        FontSize = 16,
        Padding = new Thickness(5, 0, 0, 0),
        VerticalTextAlignment = TextAlignment.Start,
        HorizontalOptions = LayoutOptions.Start,
    };
    lbl_binding.SetBinding(Label.TextProperty, "Name");

    var editDeviceBtn = new Button
    {
        Padding = 0,
        Margin = new Thickness(15, 0, 0, 0),
        FontSize = 23,
        TextColor = Color.White,
        FontFamily = "FontIcons",
        Text = "\ue706",
        BackgroundColor = Color.Gray,
        BindingContext = this,
    };
    editDeviceBtn.SetBinding(Button.AutomationIdProperty, "Id");
    
    var turnOnDeviceBtn = new Button
    {
        Padding = 0,
        Margin = new Thickness(15, 0, 0, 0),
        FontSize = 20,
        TextColor = Color.White,
        FontFamily = "FontIcons",
        Text = "\ue703",
        BackgroundColor = Color.Gray,

    };
    turnOnDeviceBtn.SetBinding(Button.AutomationIdProperty, "IP");
    
    editDeviceBtn.Clicked += (o, e) =>
    {
        //NodeMCU obj = ((Button)o).DataContext as NodeMCU;
        //NodeMCU item = (o as Button).DataContext as NodeMCU;
        Navigation.PushAsync(new NodeMCU());
    };
    turnOnDeviceBtn.Clicked += (o, e) =>
    {
        //Only way I found
        var id = lbl_binding.Text;
    };

    var grid = new Grid
    {
        Padding = new Thickness(0, 5),
        ColumnDefinitions = new ColumnDefinitionCollection()
        {
            new ColumnDefinition { Width = new GridLength(.5, GridUnitType.Star) },
            new ColumnDefinition { Width = new GridLength(.25, GridUnitType.Star) },
            new ColumnDefinition { Width = new GridLength(.25, GridUnitType.Star) }
        }
    };
    grid.Children.Add(lbl_binding, 0, 1, 0, 1);
    grid.Children.Add(editDeviceBtn, 1, 2, 0, 1);
    grid.Children.Add(turnOnDeviceBtn, 2, 3, 0, 1);

    return new ViewCell
    {
        View = grid
    };
});

最佳答案

使用BindingContext

turnOnDeviceBtn.Clicked += (o, e) =>
{
    var button = (Button)o;
    var item = (NodeMCU)button.BindingContext;
    // item will be the object bound to the selected row
};

关于android - Xamarin 获取动态 ListView DataTemplate 内的按钮单击事件数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71982847/

相关文章:

android - com.google.ads.AdView 无法实例化布局问题

Android:Jcodec 帧率和持续时间

xamarin.ios - 如何为 Mv uiCollectionViewController 提供页眉或页脚?

ios - Xamarin.Forms WebView、WKWebViewConfiguration 和发送 "Mobile" header

android - 将 Sdk 更新到 24 后,操作栏中出现不需要的空间

java - android studio 启动权限被拒绝,怎么办?

android - 在 Android 中重复将布局扩展为自定义适配器 View 的 subview

android - 禁用屏幕上的按钮 - Android Studio

android - 如何更改ListView字体颜色?

android - 布局方向改变 RTL LTR