You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Xamarin中通过代码运行时创建Grid并添加单元格输入事件

为动态创建的Grid Entry控件添加输入触发事件

实现方案

在动态创建Entry控件时,直接为其绑定输入相关事件(如实时响应的TextChanged或输入完成触发的Completed),同时通过Entry的Tag属性标记单元格的行列位置,方便在事件中区分不同输入框。

修改后的完整C#代码

using System;
using Xamarin.Forms;

namespace VarGridSample
{
    public partial class MainPage : ContentPage
    {
        private int rows { get; set; }
        private int cols { get; set; }
        private int cellcntr = 0;
        public MainPage()
        {
            InitializeComponent();
            // 注:原XAML中Cols控件的Text绑定错误,应改为Text="{Binding Cols}",或直接通过Text属性取值
        }

        private void BuildGrid_Clicked(object sender, EventArgs e)
        {
            // 清空原有Grid内容,避免重复构建导致布局混乱
            VarGrid.Children.Clear();
            VarGrid.RowDefinitions.Clear();
            cellcntr = 0;

            // 验证输入的行列数有效性
            if (!int.TryParse(Rows.Text, out rows) || !int.TryParse(Cols.Text, out cols))
            {
                DisplayAlert("提示", "请输入有效的数字行列数", "确定");
                return;
            }

            // 添加行定义
            for (int rowIndex = 0; rowIndex < rows; rowIndex++)
            {
                VarGrid.RowDefinitions.Add(new RowDefinition());
            }

            // 循环创建每个单元格的Entry控件
            for (int rowIndex = 0; rowIndex < rows; rowIndex++)
            {
                for (int colIndex = 0; colIndex < cols; colIndex++)
                {
                    var entry = new Entry
                    {
                        Text = "cell" + (cellcntr).ToString(),
                        VerticalOptions = LayoutOptions.Center,
                        HorizontalOptions = LayoutOptions.Center,
                        // 用Tag存储行列位置,方便事件中识别
                        Tag = new Tuple<int, int>(rowIndex, colIndex)
                    };

                    // 绑定实时输入变化事件
                    entry.TextChanged += OnEntryTextChanged;
                    // 绑定输入完成(回车)事件
                    entry.Completed += OnEntryCompleted;

                    VarGrid.Children.Add(entry, colIndex, rowIndex);
                    cellcntr++;
                }
            }
        }

        // 实时输入变化事件处理
        private void OnEntryTextChanged(object sender, TextChangedEventArgs e)
        {
            if (sender is Entry entry && entry.Tag is Tuple<int, int> position)
            {
                int row = position.Item1;
                int col = position.Item2;
                string oldValue = e.OldTextValue;
                string newValue = e.NewTextValue;

                // 这里替换为你的业务逻辑,比如更新数据、同步UI等
                Console.WriteLine($"[{row+1}行{col+1}列] 内容变更:{oldValue} → {newValue}");
            }
        }

        // 输入完成事件处理
        private void OnEntryCompleted(object sender, EventArgs e)
        {
            if (sender is Entry entry && entry.Tag is Tuple<int, int> position)
            {
                int row = position.Item1;
                int col = position.Item2;
                string inputContent = entry.Text;

                // 这里替换为输入完成后的业务逻辑,比如提交数据、跳转焦点等
                Console.WriteLine($"[{row+1}行{col+1}列] 输入完成,内容:{inputContent}");
            }
        }
    }
}

关键说明

  1. Grid清空逻辑:每次构建前清除原有控件和行定义,避免重复添加导致的布局重叠。
  2. 输入验证:使用int.TryParse替代直接强制转换,防止非数字输入引发崩溃。
  3. 位置标记:通过Tag存储行列的Tuple对象,在事件中可快速定位触发输入的单元格。
  4. 事件选择:
    • TextChanged:输入内容实时变化时触发,适合需要即时响应的场景。
    • Completed:用户按下回车或完成输入时触发,适合输入结束后的批量处理场景。

内容的提问来源于stack exchange,提问作者Shades49

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 10:10:49