如何为每个按钮提供唯一 ID [英] How to give unique id for each button

查看:30
本文介绍了如何为每个按钮提供唯一 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有通过循环创建的按钮,每个按钮对应一张桌子,所以当我点击一个按钮时我必须能够设计数据库的特定行

I have buttons created by loop and each button it corresponds to a desk, so i have to able when i click a button i have to design spesific row of database

我如何添加按钮:

for (int i = 1; i < terasmasasayisi; i++)
    {
        var buttonteras = new Button
        {
            Text = i.ToString(),
            HeightRequest = 45,
            WidthRequest = 45,
            Margin = 5,
            BorderRadius = 100,
        };``

        teras.Children.Add(button);
    }

如果我可以设置表号和 i(desknumber) 之间的关系,我可以根据我数据库中的 id 来执行此操作.

If i can set a relationship between the table number and i(desknumber), I can do this according to the id from my database.

我好糊涂

推荐答案

创建自定义按钮

public class MyIDButton : Button
{
    public static readonly BindableProperty ButtonIDProperty = BindableProperty.Create(nameof(ButtonId), typeof(int), typeof(MyIDButton), 999);

    public int ButtonId
    {
       get => (int)GetValue(ButtonIDProperty);
       set => SetValue(ButtonIDProperty, value);
    }
}

然后像这样使用

var buttonteras = new MyIDButton 
        {
            Text = i.ToString(),
            HeightRequest = 45,
            WidthRequest = 45,
            Margin = 5,
            ButtonId = 1, //whatever your value
            BorderRadius = 100,
        };

这篇关于如何为每个按钮提供唯一 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆